我們使用Python做加密算法如AES、MD5、SHA等時,需要用到PyCrypto模塊
PyCrypto模塊的安裝方法
1、一般在官方網站下載:
在安裝一些Python模塊時,大部分是cpython寫的模塊時會發生如下錯誤 error: Unable to find vcvarsall.bat。先前的一篇文章:在Windows上安裝Scrapy時也講到了這個問題。當時講到的方案是,安裝VS 2008進行解決,但是Vs 2008又太大,不想裝,所以這次想到了另外的方案,同樣是上次說的,當時上次很不完整。
方案一:安裝Vs2008(實測)
完全的無腦流,安裝完問題直接解決。
方案二:安裝Vs2010(2016-1-29更新)
上次在電腦上裝個Vs2010並不能像 vs2008那樣直接解決問題,主要原因是Python 2.7 使用的是 VS 2008編譯的,所以Python 2.7默認只能認出VS 2008。
解決辦法,在命令行下執行 SET VS90COMNTOOLS=%VS100COMNTOOLS%
- VS 2010 對應:SET VS90COMNTOOLS=%VS100COMNTOOLS%
- VS 2012 對應:SET VS90COMNTOOLS=%VS110COMNTOOLS%
- VS 2013 對應:SET VS90COMNTOOLS=%VS120COMNTOOLS%
或者通過修改Python的源代碼進行修改:打開“<python安裝目錄>\Lib\distutils\msvc9compiler.py”,找到 toolskey = “VS%0.f0COMNTOOLS” % version,直接修改為 toolskey = “VS100COMNTOOLS”
如果是Python 3,則上面的方法是無效的,原因是Python 3使用的是VS 2010編譯的,所以設置應該是這樣:
- VS 2010 無需設置,直接能認出
- VS 2012 對應:SET VS100COMNTOOLS=%VS110COMNTOOLS%
- VS 2013 對應:SET VS100COMNTOOLS=%VS120COMNTOOLS%
或修改msvc9compiler.py文件,將: vc_env = query_vcvarsall(VERSION, plat_spec) 中的VERSION設定為已安裝的VS版本對應的值:
- VS2008,則VERSION為9.0
- VS2010,則VERSION為10.0
- VS2012,則VERSION為11.0
- VS2013,則VERSION為12.0
- VS2014,則VERSION為13.0
注意:Python 3.5升級了distutils,默認使用_msvccompiler.py,在這個文件中可以找到:“ if version >= 14 and version > best_version: ”這里的14說明VS版本要在14以上才可以。所以根據這句,我們要安裝最新的Visual Studio2015。上面修改msvc9compiler.py的辦法沒有效果。
另外,微軟也提供了解決方案:
Python Version | You will need |
3.5 and later | Visual C++ Build Tools 2015 or Visual Studio 2015 |
3.3 and 3.4 | Windows SDK for Windows 7 and .NET 4.0 (Alternatively, Visual Studio 2010 if you have access to it) |
2.6 to 3.2 | Microsoft Visual C++ Compiler for Python 2.7 |
參考鏈接:https://blogs.msdn.microsoft.com/pythonengineering/2016/04/11/unable-to-find-vcvarsall-bat/
1.下載完成並安裝。以本機為例,安裝完成后的路徑為:
1
|
C:\Users\Administrator\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\Administrator\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
|
3.上述完成之后就可以在windwos下正常編譯python的C擴展。以pycrypto-2.6.1為例,執行如下命令
1
|
python setup.py install
|
當然也可以建立一個windows的二進制包
1
|
python setup.py bdist_wininst
|