问题
I've got a setup.py that looks something like this:
from setuptools import setup, Extension
import glob
sources = glob.glob('src/*.cpp') + glob.glob('src/*.i')
# this is ugly, but otherwise I get the wrapper included twice
sources = [source for source in sources if '_wrap' not in source]
setup(
name = 'engine',
ext_modules = [
Extension(
'_engine',
sources = sources,
swig_opts = ['-c++'],
include_dirs = ['src']
)
],
py_modules = ['engine']
package_dir = {'' : 'src'}
)
Now this works as long as I run install
twice. The first time, swig creates the engine.py in the src directory. But it doesn't get copied to the target. The second time I run the setup.py file, the engine.py gets found and is installed. Is there a way to make it all work the first time?
回答1:
I do agree that this should work out of the box, and would consider it a bug.
One option to get this working is to simply change the order in which things are build. By default, setup.py
will first collect the python modules, then build any external packages.
You can change the build order by sub-classing the default build
class, and then ask setup.py
to use your custom build
class via the cmdclass
option.
from setuptools import setup, Extension
from distutils.command.build import build as _build
#Define custom build order, so that the python interface module
#created by SWIG is staged in build_py.
class build(_build):
# different order: build_ext *before* build_py
sub_commands = [('build_ext', _build.has_ext_modules),
('build_py', _build.has_pure_modules),
('build_clib', _build.has_c_libraries),
('build_scripts', _build.has_scripts),
]
setup(
name = 'engine',
cmdclass = {'build': build }, #Use your own build class
ext_modules = [Extension('_engine',
sources = sources,
swig_opts = ['-c++'],
include_dirs = ['src']
)],
...
来源:https://stackoverflow.com/questions/17666018/using-distutils-where-swig-interface-file-is-in-src-folder