python - setup.py 没有安装 swig 扩展模块
问题描述
我正在努力弄清楚如何将 swig 生成的包装器复制到与 swig 共享库相同的级别。考虑这个树结构:
│ .gitignore
│ setup.py
│
├───hello
├───src
│ hello.c
│ hello.h
│ hello.i
│
└───test
test_hello.py
这个 setup.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',
[
'src/hello.i',
'src/hello.c'
],
include_dirs=[
"src",
],
depends=[
'src/hello.h'
],
)
],
py_modules=[
"hello"
],
)
当我这样做时,pip install .
我将在站点包上获得此内容:
>tree /f d:\virtual_envs\py364_32\Lib\site-packages\hello
D:\VIRTUAL_ENVS\PY364_32\LIB\SITE-PACKAGES\HELLO
_hello.cp36-win32.pyd
>tree /f d:\virtual_envs\py364_32\Lib\site-packages\hello_world-0.1.dist-info
D:\VIRTUAL_ENVS\PY364_32\LIB\SITE-PACKAGES\HELLO_WORLD-0.1.DIST-INFO
INSTALLER
METADATA
RECORD
top_level.txt
WHEEL
如您所见hello.py
(由 swig 生成的文件)尚未复制到site-packages
.
问题是,我已经尝试了以下类似帖子的许多答案:
不幸的是,这个问题仍然没有解决。
问题:如何修复我当前的 setup.py 以便将 swig 包装器复制到与 .pyd 文件相同的级别?
解决方案
setuptools
不能按照您想要的方式执行此操作:它只会查找py_modules
位于setup.py
的位置。恕我直言,最简单的方法是将 SWIG 模块保留在命名空间/目录结构中您希望它们的位置:重命名src
为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
py_modules
从中删除setup.py
。"hello"
列表中的将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
(和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
推荐阅读
- hybris - 如何在 Smartedit 中编辑媒体
- pulseaudio - 在带有 Alsa/Pulseaudio 的无头 Ubuntu 18.04 服务器(无 X11)上运行 Pianobar,并通过 SSH 将音频流式传输到 Mac 笔记本电脑
- neo4j - Neo4j:如何创建具有两个新关系的新节点?
- java - 如何为我的使用 mysql、java7 并在 tomcat7 上运行的 web 应用程序创建一个 setup(.exe) 文件
- reactjs - 创建生产版本时出错“不包含默认导出”
- c++ - 使用函数的 C++ 程序
- python - 如何格式化Ceaser Cipher程序python的输出
- php - 如何在 Directus 中的多对多关系上显示标签?
- rust - async fn 和 async 闭包之间的生命周期推断有什么区别?
- selenium-webdriver - .send_keys(Keys.RETURN) - python