我正在尝试从使用 pyximport 转换为通过 distutils 进行构建,我被它在放置 .so 文件的位置所做的奇怪选择所困扰。因此,我决定从 cython 文档构建教程,却发现它打印了一条消息,说明其正在构建,但什么也没做。我在virtualenv里面,cython、python2.7等都安装在里面。
首先是基础知识:
$ cython --version
Cython version 0.21.2
$ cat setup.py
from distutils.core import setup
from Cython.Build import cythonize
print "hello build"
setup(
ext_modules = cythonize("helloworld.pyx")
)
$ cat helloworld.pyx
print "hello world"
现在,当我构建它时,除了输出中的额外 src/src 内容外,一切看起来都正常:
$ python setup.py build_ext --inplace
hello build
Compiling helloworld.pyx because it changed.
Cythonizing helloworld.pyx
running build_ext
building 'src.helloworld' extension
x86_64-linux-gnu-gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fPIC -I/usr/include/python2.7 -c helloworld.c -o build/temp.linux-x86_64-2.7/helloworld.o
creating /home/henry/Projects/eyeserver/dserver/src/src
x86_64-linux-gnu-gcc -pthread -shared -Wl,-O1 -Wl,-Bsymbolic-functions -Wl,-Bsymbolic-functions -Wl,-z,relro -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -D_FORTIFY_SOURCE=2 -g -fstack-protector --param=ssp-buffer-size=4 -Wformat -Werror=format-security build/temp.linux-x86_64-2.7/helloworld.o -o /home/henry/Projects/eyeserver/dserver/src/src/helloworld.so
当我运行它时,它当然会失败:
$ echo "import helloworld" | python
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named helloworld
直到我将 .so 文件移出其额外的 src 目录:
$ mv src/helloworld.so .
$ echo "import helloworld" | python
Hello world
我做错了什么?显然我可以让构建过程移动所有的 .so 文件,但这看起来真的很 hacky。
最佳答案
每当我使用 cython 时,我都会使用 Extension
命令。
我会这样写 setup.py 文件:
from distutils.core import setup
from distutils.extension import Extension
from Cython.Build import cythonize
extensions = [
Extension("helloworld", ["helloworld.pyx"])
]
setup(
ext_modules = cythonize(extensions)
)
希望这会将 .so 文件放入当前目录。
关于python - 通过 setup.py 构建的 cython 做错事(将所有 .so 文件放在额外的 src 目录中),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27841980/