python - 通过 setup.py 构建的 cython 做错事(将所有 .so 文件放在额外的 src 目录中)

标签 python build virtualenv cython distutils

我正在尝试从使用 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/

相关文章:

java - 使用 Eclipse 类路径变量替换绝对路径 "sourcepath"?

linux - 没有足够的内存来构建 phantom.js - 怎么办?

python - 如何使用子进程运行 virtualenv 包安装

python - “Unable to locate finder for ' pip._vendor.diSTLib '” 使用"pip install virtualenv"时出错

python - 如何删除值频率小于 5 的行? python , Pandas

python - 使用 Python,如何从 Google 文档中读取纯文本?

c++ - 更改命令行 Qt5 源构建的配置的正确/快速方法

python - venv,pyvenv,pyenv,virtualenv,virtualenvwrapper,pipenv等有什么区别?

python - matplotlib 条形图的底部边框

python - 断言使用多个参数中的一个参数调用了一个方法