Hen*_*her 7 python distutils build virtualenv cython
我正在尝试从使用pyximport转换为使用distutils进行构建,而我却被它在放置.so文件的位置上的奇怪选择所困扰.因此,我决定用cython doc构建教程,但却发现它打印出一条消息说它的构建,但是没有.我在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"
Run Code Online (Sandbox Code Playgroud)
现在当我构建它时,除了输出中额外的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
Run Code Online (Sandbox Code Playgroud)
当我运行它时,它当然会失败:
$ echo "import helloworld" | python
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named helloworld
Run Code Online (Sandbox Code Playgroud)
直到我将.so文件移出其额外的src目录:
$ mv src/helloworld.so .
$ echo "import helloworld" | python
Hello world
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?显然,我可以让构建过程移动所有.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)
)
Run Code Online (Sandbox Code Playgroud)
希望这会将.so文件放在当前目录中.
| 归档时间: |
|
| 查看次数: |
2268 次 |
| 最近记录: |