setup.py未安装swig扩展模块 [英] setup.py not installing swig extension module

查看:74
本文介绍了setup.py未安装swig扩展模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力找出如何在与swig共享库相同的级别上复制swig生成的包装器.考虑这种树形结构:

I'm struggling to figure out how to copy the wrapper generated by swig at the same level than the swig shared library. Consider this tree structure:

│   .gitignore
│   setup.py
│
├───hello
├───src
│       hello.c
│       hello.h
│       hello.i
│
└───test
        test_hello.py

和这个setup.py:

and this 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 时,我将在站点软件包中获得以下内容:

When I do pip install . I'll get this content on site-packages:

>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 中.

As you can see hello.py (the file generated by swig) hasn't been copied in site-packages.

事情是,我已经从以下类似的帖子中尝试了很多答案:

Thing is, I've already tried many answers from the below similars posts:

python distutils不包含SWIG生成的模块

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

Unfortunately, the question still remains unsolved.

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

QUESTION: How can I fix my current setup.py so the swig wrapper will be copied at the same level than the .pyd file?

推荐答案

setuptools 不能按照您想要的方式进行操作:它将仅在setup.py 位于.IMHO最简单的方法是将SWIG模块保留在名称空间/目录结构中所需的位置:将 src 重命名为 hello ,然后添加 hello/__ init __.py (可以为空,也可以仅包含 hello.hello 中的所有内容),让您剩下这棵树:

setuptools cannot do this the way you want: it will look for py_modules only where setup.py is located. The easiest way IMHO is keeping the SWIG modules where you want them in the namespace/directory structure: rename src to hello, and add hello/__init__.py (may be empty or simply include everything from hello.hello), leaving you with this tree:

$ tree .
.
├── hello
│   ├── __init__.py
│   ├── _hello.cpython-37m-darwin.so
│   ├── hello.c
│   ├── hello.h
│   ├── hello.i
│   ├── hello.py
│   └── hello_wrap.c
└── setup.py

setup.py 中删除 py_modules . package 列表中的"hello" 将使 setuptools 拾取整个软件包,并包含 __ init __.py 以及生成的 hello.py :

Remove py_modules from setup.py. The "hello" in the package list will make setuptools pick up the whole package, and include __init__.py and the generated 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 development ),因此您可以将正在开发的程序包链接到venv左右..这也是 setuptools (和 distutils )工作方式的原因:dev沙箱的结构应允许直接从其运行代码,而无需移动模块周围.

This way, also .egg-linking the package works (python setup.py develop), so you can link the package under development into a venv or so. This is also the reason for the way setuptools (and distutils) works: the dev sandbox should be structured in a way that allows running the code directly from it, without moving modules around.

然后,SWIG生成的 hello.py 和生成的扩展名 _hello 将位于 hello 下:

The SWIG-generated hello.py and the generated extension _hello will then live under 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下完全相同)

(as you can see from the extension filename, I am on a Mac right now, but this works exactly the same under Windows)

此外,除了包装之外,SWIG手册中还有关于SWIG和Python名称空间和包的更多有用信息:

Also, beyond packaging, there's more useful information about SWIG and Python namespaces and packages in the SWIG manual: http://swig.org/Doc4.0/Python.html#Python_nn72

这篇关于setup.py未安装swig扩展模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆