setuptools
不能按照你想要的方式做到这一点:它会寻找py_modules
仅在哪里setup.py
位于。恕我直言,最简单的方法是将 SWIG 模块保留在命名空间/目录结构中您想要的位置:重命名src
to hello
,并添加hello/__init__.py
(可能为空或简单地包含来自hello.hello
),给你留下这棵树:
$ tree .
.
├── hello
│ ├── __init__.py
│ ├── _hello.cpython-37m-darwin.so
│ ├── hello.c
│ ├── hello.h
│ ├── hello.i
│ ├── hello.py
│ └── hello_wrap.c
└── setup.py
Remove py_modules
from setup.py
. The "hello"
in the package
清单将使setuptools
拿起整个包裹,并包括__init__.py
和生成的hello.py
:
import os
import sys
from setuptools import setup, find_packages, Extension
from setuptools.command.build_py import build_py as _build_py
class build_py(_build_py):
def run(self):
self.run_command("build_ext")
return super().run()
setup(
name='hello_world',
version='0.1',
cmdclass={'build_py': build_py},
packages = ["hello"],
ext_modules=[
Extension(
'hello._hello',
[
'hello/hello.i',
'hello/hello.c'
],
include_dirs=[
"hello",
],
depends=[
'hello/hello.h'
],
)
],
)
这样,也.egg-link
荷兰国际集团的包工作(python setup.py develop
),这样你就可以将正在开发的包链接到 venv 左右。这也是方式的原因setuptools
(and distutils
)有效:开发沙箱的结构应该允许直接从中运行代码,而无需移动模块。
SWIG 生成的hello.py
和生成的扩展名_hello
然后将生活在hello
:
>>> from hello import hello, _hello
>>> print(hello)
<module 'hello.hello' from '~/so56562132/hello/hello.py'>
>>> print(_hello)
<module 'hello._hello' from '~/so56562132/hello/_hello.cpython-37m-darwin.so'>
(从扩展文件名中可以看出,我现在使用的是Mac,但这在Windows下的工作原理完全相同)
此外,除了打包之外,SWIG 手册中还有有关 SWIG 和 Python 命名空间和包的更多有用信息:http://swig.org/Doc4.0/Python.html#Python_nn72 http://swig.org/Doc4.0/Python.html#Python_nn72