2015-03-30 64 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

相關問題