安裝成功之后,再修改設置。讓Cython可以找到vcarsall.bat。此處有兩種方案。(我采用方案1,親測可用。方案2未測試,看似可用。)
windows平台使用Microsoft Visual C++ Compiler for Python 2.7編譯python擴展安裝Cython
可以使用pip命令安裝Cython。
pip install cython
處理vcvarsall.bat
若不處理,可能會出現“Unable to find vcvarsall.bat”錯誤。
在windows平台上安裝python c extension的擴展包是件很痛苦的事情,一般通過安裝vc/vs系列來編譯C擴展,不過安裝包都比較大。或者通過mingw編譯,不過有時會在兼容性上出現點問題。
有個好消息就是微軟為Python提供了專用的編譯器Microsoft Visual C++ Compiler for Python 2.7(包含32位和64位) 下載地址: http://aka.ms/vcpython27
1.下載完成並安裝。以本機為例,安裝完成后的路徑為:
1
|
C:\Users\acer\AppData\Local\Programs\Common\Microsoft\Visual C++ for Python\9.0
|
2.修改python安裝目錄下Lib\distutils\msvc9compiler.py文件(如有必要可能msvccompiler.py文件也需要做相應更改,視系統而定),找到get_build_version方法直接return 9.0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
def
get_build_version():
"""Return the version of MSVC that was used to build Python.
For Python 2.3 and up, the version number is included in
sys.version. For earlier versions, assume the compiler is MSVC 6.
"""
return 9.0
prefix
=
"MSC v."
i
=
sys.version.find(prefix)
if
i
=
=
-
1
:
return
6
i
=
i
+
len
(prefix)
s, rest
=
sys.version[i:].split(
" "
,
1
)
majorVersion
=
int
(s[:
-
2
])
-
6
minorVersion
=
int
(s[
2
:
3
])
/
10.0
# I don't think paths are affected by minor version in version 6
if
majorVersion
=
=
6
:
minorVersion
=
0
if
majorVersion >
=
6
:
return
majorVersion
+
minorVersion
# else we don't know what version of the compiler this is
return
None
|
然后再找到find_vcvarsall方法直接返回vcvarsall.bat的路徑(以自己機器安裝后的路徑為准)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
def
find_vcvarsall(version):
"""Find the vcvarsall.bat file
At first it tries to find the productdir of VS 2008 in the registry. If
that fails it falls back to the VS90COMNTOOLS env var.
"""
return r 'C:\Users\acer\AppData\Local\Programs\Common\Microsoft\Visual C++ for Python\9.0\vcvarsall.bat'
vsbase
=
VS_BASE
%
version
try
:
productdir
=
Reg.get_value(r
"%s\Setup\VC"
%
vsbase,
"productdir"
)
except
KeyError:
productdir
=
None
# trying Express edition
if
productdir
is
None
:
vsbase
=
VSEXPRESS_BASE
%
version
try
:
productdir
=
Reg.get_value(r
"%s\Setup\VC"
%
vsbase,
"productdir"
)
except
KeyError:
productdir
=
None
log.debug(
"Unable to find productdir in registry"
)
if
not
productdir
or
not
os.path.isdir(productdir):
toolskey
=
"VS%0.f0COMNTOOLS"
%
version
toolsdir
=
os.environ.get(toolskey,
None
)
if
toolsdir
and
os.path.isdir(toolsdir):
productdir
=
os.path.join(toolsdir, os.pardir, os.pardir,
"VC"
)
productdir
=
os.path.abspath(productdir)
if
not
os.path.isdir(productdir):
log.debug(
"%s is not a valid directory"
%
productdir)
return
None
else
:
log.debug(
"Env var %s is not set or invalid"
%
toolskey)
if
not
productdir:
log.debug(
"No productdir found"
)
return
None
vcvarsall
=
os.path.join(productdir,
"vcvarsall.bat"
)
if
os.path.isfile(vcvarsall):
return
vcvarsall
log.debug(
"Unable to find vcvarsall.bat"
)
return
None
|
方案2:修改注冊表
錯誤描述: 在從源代碼安裝Python模塊時遇到此錯誤。可是我明明從官網下載並安裝了Microsoft Visual C++ Compiler Package for Python 2.7,且配置了環境變量path。 錯誤原因: 報這個錯誤的原因是Python的distutils模塊中的msvc9compiler.py並不從環境變量指定的路徑中尋找’vcvarsall.bat’,而是通過注冊表來尋找…,然而,不知為什么編譯器安裝過程沒有配置注冊表。 解決辦法: 只要手工把注冊表配置好,就可以了。 // 1、打開注冊表編輯器 run regedit // 2、配置 // 2.1、如果你安裝的Python是32位的,則,創建如下項: HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\9.0\Setup\VC // 2.2、如果你安裝的Python是64位的,則,創建如下項: HKEY_CURRENT_USER\Software\Wow6432Node\Microsoft\VisualStudio\9.0\Setup\VC // 3、並在此項下新建字符串值: 名稱:productdir 數據:vcvarsall.bat所在路徑 注意:路徑中不包含最后的反斜杠。
3、創建工作目錄並生成pyd文件
這里有一個坑。程序所在的目錄路徑不能包含中文文字。所以我在E盤下創建一個test文件夾,用於放置要處理的python文件。
簡單寫了一個測試文件(命名為test.py):
-
#coding:utf-8 def hello(): print("Hello world") input("<press ENTER to quit>")
在該目錄下,再新建一個py文件(命名為setup.py):
-
from distutils.core import setup from Cython.Build import cythonize setup( name = 'Hello world app', ext_modules = cythonize("test.py"), )
接着,再打開cmd,跳到該目錄並執行如下命令:
- python setup.py build_ext --inplace
參考博客地址:http://yshblog.com/blog/117 ,https://blog.csdn.net/qq_34106574/article/details/81166062