2015-03-30 96 views
0

我在64位Windows机器上使用Anacondas。Cython Hello World示例适用于Anacondas Python 3.3,但不适用于3.4

我编译了一个hello world Cython的例子。它是在文件hello.pyx,并包含:

def say_hello_to(name): 
    print("Hello %s!" % name) 

我使用run_hello.py

import pyximport; pyximport.install() 
import hello as hello 

hello.say_hello_to('jon') 

安装文件是setup.py运行它:

from distutils.core import setup 
from Cython.Build import cythonize 

setup(
    name = 'Hello world app', 
    ext_modules = cythonize("hello.pyx"), 
) 

我然后使用以下代码在Anacondas上编译Python 3.3中的代码:

> activate py33 
> python setup.py build_ext --inplace 

(请注意,py33是我的Python 3.3环境)

我可以运行的例子: “你好!乔恩”

python run_hello.py 

打印出如预期。

现在,如果我在我的环境改变到Python 3.4和编译:

> activate py34 
> python setup.py build_ext --inplace 

我没有得到任何错误,并且外壳显示

running build_ext 

但是,如果我尝试从运行run_hello.py在PY34环境:

python run_hello.py 

我得到:

Traceback (most recent call last): 
    File "run_hello.py", line 2, in <module> 
    import hello as hello 
ImportError: DLL load failed: The specified module could not be found. 

该错误不是很具描述性。我能做些什么来帮助我在Python 3.4上完成这项工作?


如果我删除的hello.c从我的硬盘驱动器/生成文件夹,试图从Python来编译3.4回报:

Compiling hello.pyx because it changed. 
Cythonizing hello.pyx 
running build_ext 
building 'hello' extension 
creating build 
creating build\temp.win-amd64-3.4 
creating build\temp.win-amd64-3.4\Release 
C:\Anaconda\envs\py34\Scripts\gcc.bat -mdll -O -Wall -IC:\Anaconda\envs\py34\include -IC:\Anaconda\envs\py34\include -c hello.c -o build\temp.win-amd64-3.4\Release\hello.o 
writing build\temp.win-amd64-3.4\Release\hello.def 
C:\Anaconda\envs\py34\Scripts\gcc.bat -shared -s build\temp.win-amd64-3.4\Release\hello.o build\temp.win-amd64-3.4\Release\hello.def -LC:\Anaconda\envs\py34\libs -LC:\Anaconda\envs\py34\PCbuild\amd64 
-lpython34 -lmsvcr100 -o c:\Users\Jon\Documents\GitHub\CythonFunctions\example1\hello.pyd 
build\temp.win-amd64-3.4\Release\hello.o:hello.c:(.text+0x314): undefined reference to `__imp__PyThreadState_Current' 
build\temp.win-amd64-3.4\Release\hello.o:hello.c:(.text+0x493): undefined reference to `__imp__Py_NoneStruct' 
build\temp.win-amd64-3.4\Release\hello.o:hello.c:(.text+0x97b): undefined reference to `__imp_PyExc_ImportError' 
collect2.exe: error: ld returned 1 exit status 
error: command 'C:\\Anaconda\\envs\\py34\\Scripts\\gcc.bat' failed with exit status 1 

如果我与Python 3.3一样,我得到:

Compiling hello.pyx because it changed. 
Cythonizing hello.pyx 
running build_ext 
building 'hello' extension 
creating build 
creating build\temp.win-amd64-3.3 
creating build\temp.win-amd64-3.3\Release 
C:\Anaconda\envs\py33\Scripts\gcc.bat -DMS_WIN64 -mdll -O -Wall -IC:\Anaconda\envs\py33\include -IC:\Anaconda\envs\py33\include -c hello.c -o build\temp.win-amd64-3.3\Release\hello.o 
writing build\temp.win-amd64-3.3\Release\hello.def 
C:\Anaconda\envs\py33\Scripts\gcc.bat -DMS_WIN64 -shared -s build\temp.win-amd64-3.3\Release\hello.o build\temp.win-amd64-3.3\Release\hello.def -LC:\Anaconda\envs\py33\libs -LC:\Anaconda\envs\py33\PCb 
uild\amd64 -lpython33 -lmsvcr100 -o c:\Users\Jon\Documents\GitHub\CythonFunctions\example1\hello.pyd 

谁遇到一些其他用户“gcc.bat失败,退出状态1”已发现的问题是由于32/64位冲突。

在py33版本的编译数据中,gcc.bat参数中有-DMS_WIN64,但它不在py34参数中。那可能是导致我的问题的原因吗?如果是这样,我怎么得到py34来添加它?

回答

0

它看起来像py33环境使用Visual Studio编译(-lmsvcr100)。这可能是因为它没有安装libpython conda软件包,导致distutils使用mingw(gcc)编译而不是Visual Studio。 conda remove libpython可能会解决您的Python 3.4环境的问题。

+0

谢谢,但py33和py34都使用-lmsvcr100选项。 – Ginger 2015-04-01 07:39:13

+0

哦,对不起,我没有在3.4输出中看到它。是否删除libpython修复它? – asmeurer 2015-04-01 18:27:16

+0

我尝试删除它,但它似乎并未安装在我的py34环境中。 – Ginger 2015-04-02 18:56:27

2

我一直在使用TDM-GCC作为Python 2.7 64位的64位编译器几年没有任何问题,但是在安装Python 3.4 64位后,我确实遇到了编译Cython模块的问题。

我相信我用mingw-w64-for-python解决了这个问题。 (也看看这个github issue)。

有一个自述文件(目前是mingwpy-2015-04-readme.pdf),所以很简单。但简要地说,你需要做的:

  1. 下载mingwpy x86-64的工具链(64位)(目前mingw64static-2014-11.tar.xz
  2. 解压缩文件到一个目录(例如,C:\mingw64static
  3. C:\Anaconda3\Scripts前面添加C:\mingw64static\bin在你的Path 环境变量。
  4. 按照readme文件中的说明下载libpython。 (我用 libpython-cp34-none-win_amd64.7z为Python 3.4 64位)在存档
  5. 解压,并复制文件(libmsvcr100.alibpython34.dll.a)为C:\Python\libs\目录(蟒蛇,它是C:\Anaconda3\libs
  6. C:\Python\Lib\distutils创建一个名为distutils.cfg文件 目录(阿纳康达,它是C:\Anaconda3\Lib\distutils)与 下列内容:

    [build] 
    
    compiler=mingw32 
    

现在python会在编译你的cython模块时正确使用mingw-w64

相关问题