Code Monkey home page Code Monkey logo

cppimport's Introduction

If you've used cppimport version 0.0.*, some new features for you! Compiler arguments, multiple source files, bug fixes! Read on.

Import C or C++ files directly from Python!

Let's try it out. First, if you're on Linux or OS X, install with the terminal command pip install cppimport.

Most cppimport users combine it with pybind11, but you can use a range of methods to create your Python extensions. Raw C extensions, Boost.Python, SWIG all work. Let's look at a simple C++ extension:

/*
<%
setup_pybind11(cfg)
%>
*/
#include <pybind11/pybind11.h>

namespace py = pybind11;

int square(int x) {
    return x * x;
}

PYBIND11_PLUGIN(somecode) {
    pybind11::module m("somecode", "auto-compiled c++ extension");
    m.def("square", &square);
    return m.ptr();
}

Save this code as somecode.cpp.

Open a python interpreter and run these lines [1]:

>>> import cppimport
>>> somecode = cppimport.imp("somecode") #This will pause for a moment to compile the module
>>> somecode.square(9)
81

I'm a big fan of the workflow that this enables, where you can edit both C++ files and Python and recompilation happens transparently.

What's actually going on?

The technical description: cppimport looks for a C or C++ source file that matches the requested module. If such a file exists, the file is first run through the Mako templating system. The compilation options produced by the Mako pass are then use to compile the file as a Python extension. The extension (shared library) that is produced is placed in the same folder as the C++ source file. Then, the extension is loaded.

Simpler language please: Sometimes Python just isn't fast enough. Or you have existing code in a C++ library. So, you write a Python extension module, a library of compiled code. I recommend pybind11 for C++ to Python bindings or cffi for C to Python bindings. I've done this a lot over the years. But, I discovered that my productivity goes through the floor when my development process goes from Edit -> Test in just Python to Edit -> Compile -> Test in Python plus C++. So, cppimport combines the process of compiling and importing an extension in Python so that you can type modulename = cppimport.imp("modulename") and not have to worry about multiple steps. Internally, cppimport looks for a file modulename.cpp. If one is found, it's run through the Mako templating system to gather compiler options, then it's compiled and loaded as an extension module.

Note that because of the Mako pre-processing, the comments around the configuration block may be omitted.

Recompilation only happens when necessary:

Compilation should only happen the first time the module is imported. The C++ source is compared with a checksum on each import to determine if the file has changed. Additional dependencies (header files!) can be tracked by adding to the Mako header:

cfg['dependencies'] = ['file1.h', 'file2.h']

I need to set the compiler or linker args!

cfg['linker_args'] = ['...']
cfg['compiler_args'] = ['...']
cfg['libraries'] = ['...']
cfg['include_dirs'] = ['...']

For example, to use C++11, add:

<%
cfg['compiler_args'] = ['-std=c++11']
%>

I want multiple source files for one extension!

cfg['sources'] = ['...']

I need more output!

Calling cppimport.set_quiet(False) will result in output that will be helpful in debugging compile errors.

Sometimes I need to force a rebuild even when the checksum matches

Call cppimport.force_rebuild() before running cppimport.imp(...).

I want incremental compiles on extensions with multiple sources.

(For the uninitiated, incremental compilation involves only recompiling those source files that have changed or include headers that have changed.)

cppimport is built on top of the setuptools and distutils, the standard library for python packaging and distribution. Unfortunately, setuptools does not support incremental compilation. I recommend following the suggestions on this SO answer. That is:

  1. Use ccache to (massively) reduce the cost of rebuilds
  2. Enable parallel compilation. This can be done with cfg['parallel'] = True in the C++ file's configuration header.

I need information about filepaths in my module configuration code!

The module name is available as the fullname variable and the C++ module file is available as filepath. For example,

<%
module_dir = os.path.dirname(filepath)
%>

Windows?

I don't know if cppimport works on Windows. If you're on Windows, try it out and I'll happily accept a pull request for any issues that you fix. I have reports that cppimport works on Windows with Python 3.6 and Visual C++ 2015 Build Tools.

cppimport uses the MIT License

cppimport's People

Contributors

evertheylen avatar fran6co avatar mivade avatar nrhinehart avatar oysstu avatar tbenthompson avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.