安装Cython
可使用pip命令安装Cython。windows
pip install cython
处理vcvarsall.bat
若不处理,可能会出现“Unable to find vcvarsall.bat”错误。app
在windows平台上安装python c extension的扩展包是件很痛苦的事情,通常经过安装vc/vs系列来编译C扩展,不过安装包都比较大。或者经过mingw编译,不过有时会在兼容性上出现点问题。编辑器
有个好消息就是微软为Python提供了专用的编译器Microsoft Visual C++ Compiler for Python 2.7(包含32位和64位) 下载地址: http://aka.ms/vcpython27post
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.0ui
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的路径(以本身机器安装后的路径为准)this
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’,而是经过注册表来寻找…,然而,不知为何编译器安装过程没有配置注册表。 解决办法: 只要手工把注册表配置好,就能够了。 // 一、打开注册表编辑器 run regedit // 二、配置 // 2.一、若是你安装的Python是32位的,则,建立以下项: HKEY_CURRENT_USER\Software\Microsoft\VisualStudio\9.0\Setup\VC // 2.二、若是你安装的Python是64位的,则,建立以下项: HKEY_CURRENT_USER\Software\Wow6432Node\Microsoft\VisualStudio\9.0\Setup\VC // 三、并在此项下新建字符串值: 名称:productdir 数据:vcvarsall.bat所在路径 注意:路径中不包含最后的反斜杠。
三、建立工做目录并生成pyd文件
这里有一个坑。程序所在的目录路径不能包含中文文字。因此我在E盘下建立一个test文件夹,用于放置要处理的python文件。url
简单写了一个测试文件(命名为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