I wrote a Python C extension that I'm building with distutils. This is setup.py
:
from distutils.core import setup, Extension
utilsmodule = Extension('utils', sources = ['utilsmodule.c'])
setup (name = 'myPackage',
version = '1.0',
description = 'My package',
ext_modules = [utilsmodule])
When I run python setup.y build
, the extension is built correctly but the .pyd
file goes into the folder build/lib.win-amd64-3.7
, and not into the module's root where Python looks for modules to import. I need to move that file out of build
after building to be able to import it.
I thought about adding a line after setup()
that moves the file, but that seems a bit dirty, I'm guessing that distutils should do that work.
What is the right way to compile an extension in a way that it can be imported by other Python files immediately after build?
distutils should do that work.
It shouldn't. Building is just an intermediate phase to packaging, installing and using.
What is the right way to compile an extension in a way that it can be imported by other Python files immediately after build?
There is no such a way. You can set %PYTHONPATH%
to point to build/lib.win-amd64-3.7
and import the module directly from build/
.
Or you may try to bend distutils:
from distutils.core import setup
from distutils.command.build_ext import build_ext as _build_ext
import os
class build_ext(_build_ext):
def run(self):
_build_ext.run(self)
os.rename("build/lib.win-amd64-3.7/%s" % mypydname, dest_dir)
setup(
…
cmdclass={'build_ext': build_ext},
…
)
but any way you have to decide where to move the compiled module.