setup.py 未安装 swig 扩展模块

2024-02-08

我正在努力弄清楚如何将 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:在执行其他操作之前运行 build_ext https://stackoverflow.com/questions/29477298/setup-py-run-build-ext-before-anything-else

  • python distutils 不包含 SWIG 生成的模块 https://stackoverflow.com/questions/12491328/python-distutils-not-include-the-swig-generated-module

不幸的是,这个问题仍然没有解决。

问题:如何修复当前的 setup.py,以便 swig 包装器将被复制到与 .pyd 文件相同的级别?


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

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

setup.py 未安装 swig 扩展模块 的相关文章

随机推荐