无法从conda环境构建cython代码

3 投票
1 回答
2074 浏览
提问于 2025-04-18 17:08

我在Windows上使用Anaconda。我的根环境里有Python 2.7,还有一个叫py34的备用环境,里面是Python 3.4。对于Cython我还是个新手,正在尝试用它来做一个需要和C语言连接的项目。我在根环境中可以顺利构建和运行Cython的示例,但在py34环境中却失败了。

在根环境中运行时,一切正常:

C:\ETC\py34>python setup.py build_ext --inplace
Compiling hello.pyx because it changed.
Cythonizing hello.pyx
running build_ext
building 'hello' extension
c:\Anaconda\Scripts\gcc.bat -DMS_WIN64 -mdll -O -Wall -Ic:\Anaconda\include -Ic:\Anaconda\PC -c hello.c -o build\temp.wi
n-amd64-2.7\Release\hello.o
writing build\temp.win-amd64-2.7\Release\hello.def
c:\Anaconda\Scripts\gcc.bat -DMS_WIN64 -shared -s build\temp.win-amd64-2.7\Release\hello.o build\temp.win-amd64-2.7\Rele
ase\hello.def -Lc:\Anaconda\libs -Lc:\Anaconda\PCbuild\amd64 -lpython27 -lmsvcr90 -o C:\ETC\py34\hello.pyd

但是在Python 3.4环境中就失败了:

[py34] C:\ETC\py34>python setup.py build_ext --inplace
running build_ext
error: [WinError 2] El sistema no puede encontrar el archivo especificado

看起来“activate py34”这个命令没有正确设置Cython所需的所有路径。

请帮帮我。

更新:问题似乎和Python 3.4有关,而不是因为这是一个conda环境。我创建了一个新的py33环境,里面是Python 3.3,结果一切正常。对我来说,这个解决方案已经足够了,我希望我的项目支持Python 3.xx。至于对3.4的支持,可以等一等。可能现在正在进行修复Cython对Python 3.4支持的工作。

1 个回答

1

这是一个老问题,但我昨天遇到了类似的情况。我的解决办法是在setup.py文件里加了一个开关,这样就可以把LIBRARY_INCLIBRARY_LIB的路径添加到distutils的include_dirslibrary_dirs里。

import platform
from distutils.core import setup, Extension

if platform.system() == 'Windows':
    try:
        include_dirs = [os.environ['LIBRARY_INC']]
    except KeyError:
        include_dirs = []
    try:
        library_dirs = [os.environ['LIBRARY_LIB']]
    except KeyError:
        library_dirs = []

...
setup(ext_modules=[
        Extension(...
                include_dirs=include_dirs,
                libraries=...,
                library_dirs=library_dirs)

撰写回答