2010-11-18 45 views
1

我試圖建立一個與Python的下PC \ example_nt源分佈使用MS編譯器

我複製example.c和setup.py的目錄C例如建築物上的窗戶一個Python模塊:\ mymod

當我運行C:\Python27\python.exe setup.py install我得到錯誤....

error: Unable to find vcvarsall.bat

我做了一些在distutils的挖四周,發現這是微軟的Visual Studio,但只有我的9版本之後會有版本8.顯然,它試圖獲得版本9,因爲在C:\ Python27下編譯了python。

我修改了setup.py,並在最上面放置了以下內容。

from distutils import msvc9compiler 
msvc9compiler.VERSION = 8.0 

這樣我就能夠編譯後,得到了以下....

C:\mymod>C:\Python27\python.exe setup.py install 
running install 
running build 
running build_ext 
building 'example' extension 
creating build 
creating build\temp.win32-2.7 
creating build\temp.win32-2.7\Release 
C:\Program Files\Microsoft Visual Studio 8\VC\BIN\cl.exe /c /nologo /Ox /MD /W3 
/GS- /DNDEBUG -IC:\Python27\include -IC:\Python27\PC /Tcexample.c /Fobuild\temp. 
win32-2.7\Release\example.obj 
example.c 
creating build\lib.win32-2.7 
C:\Program Files\Microsoft Visual Studio 8\VC\BIN\link.exe /DLL /nologo /INCREME 
NTAL:NO /LIBPATH:C:\Python27\libs /LIBPATH:C:\Python27\PCbuild /EXPORT:initexamp 
le build\temp.win32-2.7\Release\example.obj /OUT:build\lib.win32-2.7\example.pyd 
/IMPLIB:build\temp.win32-2.7\Release\example.lib /MANIFESTFILE:build\temp.win32 
-2.7\Release\example.pyd.manifest 
    Creating library build\temp.win32-2.7\Release\example.lib and object build\te 
mp.win32-2.7\Release\example.exp 
C:\Program Files\Microsoft Visual Studio 8\VC\BIN\mt.exe -nologo -manifest build 
\temp.win32-2.7\Release\example.pyd.manifest -outputresource:build\lib.win32-2.7 
\example.pyd;2 
running install_lib 
copying build\lib.win32-2.7\example.pyd -> C:\Python27\Lib\site-packages 
running install_egg_info 
Removing C:\Python27\Lib\site-packages\example-1.0-py2.7.egg-info 
Writing C:\Python27\Lib\site-packages\example-1.0-py2.7.egg-info 

現在,當我運行C:\ Python27 \ python.exe,並嘗試import example我得到以下...

ImportError: DLL load failed: The specified module could not be found. 

我做錯了什麼? VS8不支持創建Python 2.7模塊嗎? 我該怎麼辦?

最終我需要爲某些Windows C庫構建綁定,以便我可以使用Python來擴展某些專有程序而不是C.我必須使用VS8來創建C擴展。那麼這些離我而去。

請諮詢。

感謝, 〜埃裏克

+0

如果您必須使用VS8並希望使用Python 2.7,請使用VS8構建您自己的版本。不知道這可能會對您可能想要使用的任何其他第三方擴展有什麼影響,除非您可以重建它們。 – martineau 2010-11-18 20:12:39

回答

1

一般來說,你必須建立使用VS的相同版本的蟒蛇與內置的Python模塊。您有幾種選擇:

  1. 使用python2.6的,這點我覺得 VS8(或更早的版本,我敢肯定,有2.5和2.6之間的變化)
  2. 使用VS9。我認爲你不能這樣做,因爲你使用的專有庫是用VS8編譯的。與python真的發生同樣的問題。
  3. 使用ctypes創建綁定。這可能很難,而且很容易讓你的程序崩潰。
  4. 使用VS8從源代碼構建Python2.7。如果由於某種原因你不能使用Python2.6,那麼這可能是最好的選擇。

我會推薦選項1,如果它的工作。

+0

您可以從Microsoft [here](http://www.microsoft.com/downloads/en/details.aspx?FamilyId=F3FBB04E-92C2-4701-B4BA-92E26E408569&displaylang=en)下載VS 2008的免費版本。我相信這是用於編譯Python 2.7的版本。不知道它是否可以在同一個系統上與另一個版本共存... – martineau 2010-11-18 20:04:43

+0

謝謝。我一定會給#1一個去。我已經嘗試#4,我可以加載使用Visual Studio創建的示例pyd文件。當我去給調用庫例程的模塊添加另一個函數時,我變得無法在pyhon中導入包,並得到與之前相同的導入錯誤。 – 2010-11-19 12:37:45