Search code examples
pythonpython-2.7cythondistutilspythonpath

Should PYTHONPATH include ./build/*?


Running

$ python setup.py build_ext

with the usual Cython extension configuration creates a build directory and places the compiled modules deep within it.

How is the Python interpreter supposed to find them now? Should PYTHONPATH include those sub-sub-directories? It seems kludgy to me. Perhaps this is meant to work differently?


Solution

  • Presumably, when you write a package containing Cython code, your setup.py will contain something similar to this:

    setup(
        ext_modules = cythonize("example.pyx")
    )
    

    (there are some variations, but that's the general idea). When you run

    python setup.py install
    

    or

    python setup.py install --user
    

    You will see it creates binary files (with extensions based on your OS - on mine it will be example.so) and copies them to the standard installation directory (also depending on your OS).

    These binary files are therefore already in the import path of your Python distribution, and it can import them like regular modules.

    Consequently, you do not need to add the build directory to the path. Just install (possibly with --user, or use virtualenv, if you're developing), and let the extensions be imported the regular way.