須要安裝 Microsoft Visual Studio2017 Community 或者buildtoolspython
1.設置Microsoft Visual Studio 14.0的環境變量 VCINSTALLDIR 變量值 C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC(默認安裝位置,請根據本身的安裝位置肯定)如圖:git
2.在上面安裝路徑中執行 vcvarsall.bat 腳本github
3.在命令行(cmd)中執行 set CL=-FI"%VCINSTALLDIR%\INCLUDE\stdint.h"dom
4.從新安裝 pip install pycryptoide
5.安裝好後,須要進入Lib\site-packages\文件夾中將文件夾crypto 的第一個字母變爲大寫 "Crypto"(切記切記,不然代碼會告訴import不成功)函數
(1)本機上安裝了Microsoft Visual Studio2017 Community版ui
A、將D:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.10.25017\include\stdint.h文件拷貝到C:\Program Files (x86)\Windows Kits\10\Include\10.0.15063.0\ucrt\目錄下加密
B、修改C:\Program Files (x86)\Windows Kits\10\Include\10.0.15063.0\ucrt\inttypes.h中的第13行,將spa
修改成.net
--------------------
最近接觸公司後臺管理系統的開發,其中涉及到加密模塊pycrypto。
重點來了!!!!敲黑板!!!!
pycrypto在PyCharm中跟其餘的模塊不同,pip install pycrypto安裝的是1.4.1版本,而後雖然模塊可以install成功,但實際上代碼仍是沒法關聯起來,運行會報異常。
百度解決方案:下載pycrypto源碼安裝
不過安裝pycrypto源碼可不是這麼簡單的事情,這裏把這幾天踩過的坑都記錄一下。
1.下載pycrypto源碼
官網:https://www.dlitz.net/software/pycrypto/
github:https://github.com/dlitz/pycrypto
下載源碼解壓,win+R運行cmd,進入解壓文件夾
python setup.py install
1
若是沒有報錯誤,看到
running install_egg_info
1
恭喜你,不用踩坑,直接能夠關閉這篇東西去愉快地寫代碼了
2.安裝過程當中顯示unable to find vcvarsall.bat
這是window環境下安裝模塊比較常見的問題,這時候你百度的話大部分文章就會告訴你下載vs,python2的話安裝vs2010,python3的話安裝vs2013
重點!!!!!!!!
不要着急去安裝vs,這東西一安裝就是好幾個小時,在這裏我對vs裝了卸,卸了裝浪費了一天半的時間。
python有個深坑!不一樣的python版本對應不一樣的vs版本,具體對應的版本在python所在的路徑下\Lib\distutils_msvccompiler.py
好比個人路徑是E:\Python\Python36\Lib\distutils_msvccompiler.py
打開找到def _find_vcvarsall(plat_spec)
這個函數下有個if version >= 14 and version > best_version
這裏的version >= 14就是對應的vs版本號,若是你跟我同樣是python3.6的話應該這裏是14,以前就是看到其餘的博客,沒有查看這裏的版本號就裝,先是裝了vs2010,而後裝2013,再卸了裝了對應版本號的vs2015,其中vs的版本號對應關係以下:
msvs2005版本號爲8.0,對應環境變量VS80COMNTOOLS
msvs2008版本號爲9.0,對應環境變量VS90COMNTOOLS
msvs2010版本號爲10.0,對應環境變量VS100COMNTOOLS
msvs2012版本號爲11.0,對應環境變量VS110COMNTOOLS
msvs2013版本號爲12.0,對應環境變量VS120COMNTOOLS
msvs2015版本號爲14.0,對應環境變量VS140COMNTOOLS
3.安裝對應版本的msvs(Microsoft Visual Studio)
若是是Visual Studio 2015,提供個百度經驗上百度網盤給你們
http://pan.baidu.com/s/1eSueWLG
若是不須要用vs進行開發的話,裝community免費版就能夠了,不必糾結專業版啥的
安裝過程當中要注意,建議安裝的時候選自定義(截圖來自百度經驗,由於已經安裝了打開以後有點不太同樣)
記得勾選VC++2015和Python Tools。Web Deveolper Tools的話最好也勾上吧。
但個人安裝過程沒這麼順利,淚奔~
安裝部分模塊失敗,而後我從新安裝。由於主要的vs2015安裝是成功的,因此只勾選
VC++2015
Python Tools
Web Deveolper Tools
這三個模塊,500M左右,從新安裝就提示所有成功了
不少人說裝完以後要改環境變量,
python2:set VS90COMNTOOLS=%VS140COMNTOOLS%
python3:set VS100COMNTOOLS=%VS140COMNTOOLS%
1
2
可能python3.6版本高了不須要這樣,不過也記錄一下給各位看官僅供參考。反正我是不須要改,由於接下來我踩了另外一個坑
再次進入pycryto的解壓文件夾python setup.py install 仍然顯示
building ‘Crypto.Random.OSRNG.winrandom‘ extension
running install
running build
running build_py
running build_ext
warning: GMP or MPIR library not found; Not building Crypto.PublicKey._fastmath.
building ‘Crypto.Random.OSRNG.winrandom‘ extension
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\BIN\x86_amd64\cl.exe /c /nologo /Ox /W3 /GL /DNDEBUG /MD -Isrc/ -Isrc/inc-msvc/ -IC:\Python36\include -IC:\Python36\include
winrand.c
C:\Program Files (x86)\Windows Kits\10\include\10.0.10240.0\ucrt\inttypes.h(26): error C2061: syntax error: identifier ‘intmax_t‘
C:\Program Files (x86)\Windows Kits\10\include\10.0.10240.0\ucrt\inttypes.h(27): error C2061: syntax error: identifier ‘rem‘
C:\Program Files (x86)\Windows Kits\10\include\10.0.10240.0\ucrt\inttypes.h(27): error C2059: syntax error: ‘;‘
C:\Program Files (x86)\Windows Kits\10\include\10.0.10240.0\ucrt\inttypes.h(28): error C2059: syntax error: ‘}‘
C:\Program Files (x86)\Windows Kits\10\include\10.0.10240.0\ucrt\inttypes.h(30): error C2061: syntax error: identifier ‘imaxdiv_t‘
C:\Program Files (x86)\Windows Kits\10\include\10.0.10240.0\ucrt\inttypes.h(30): error C2059: syntax error: ‘;‘
C:\Program Files (x86)\Windows Kits\10\include\10.0.10240.0\ucrt\inttypes.h(40): error C2143: syntax error: missing ‘{‘ before ‘__cdecl‘
C:\Program Files (x86)\Windows Kits\10\include\10.0.10240.0\ucrt\inttypes.h(41): error C2146: syntax error: missing ‘)‘ before identifier ‘_Number‘
C:\Program Files (x86)\Windows Kits\10\include\10.0.10240.0\ucrt\inttypes.h(41): error C2061: syntax error: identifier ‘_Number‘
C:\Program Files (x86)\Windows Kits\10\include\10.0.10240.0\ucrt\inttypes.h(41): error C2059: syntax error: ‘;‘
C:\Program Files (x86)\Windows Kits\10\include\10.0.10240.0\ucrt\inttypes.h(42): error C2059: syntax error: ‘)‘
C:\Program Files (x86)\Windows Kits\10\include\10.0.10240.0\ucrt\inttypes.h(45): error C2143: syntax error: missing ‘{‘ before ‘__cdecl‘
C:\Program Files (x86)\Windows Kits\10\include\10.0.10240.0\ucrt\inttypes.h(46): error C2146: syntax error: missing ‘)‘ before identifier ‘_Numerator‘
C:\Program Files (x86)\Windows Kits\10\include\10.0.10240.0\ucrt\inttypes.h(46): error C2061: syntax error: identifier ‘_Numerator‘
C:\Program Files (x86)\Windows Kits\10\include\10.0.10240.0\ucrt\inttypes.h(46): error C2059: syntax error: ‘;‘
C:\Program Files (x86)\Windows Kits\10\include\10.0.10240.0\ucrt\inttypes.h(46): error C2059: syntax error: ‘,‘
C:\Program Files (x86)\Windows Kits\10\include\10.0.10240.0\ucrt\inttypes.h(48): error C2059: syntax error: ‘)‘
C:\Program Files (x86)\Windows Kits\10\include\10.0.10240.0\ucrt\inttypes.h(50): error C2143: syntax error: missing ‘{‘ before ‘__cdecl‘
C:\Program Files (x86)\Windows Kits\10\include\10.0.10240.0\ucrt\inttypes.h(56): error C2143: syntax error: missing ‘{‘ before ‘__cdecl‘
C:\Program Files (x86)\Windows Kits\10\include\10.0.10240.0\ucrt\inttypes.h(63): error C2143: syntax error: missing ‘{‘ before ‘__cdecl‘
C:\Program Files (x86)\Windows Kits\10\include\10.0.10240.0\ucrt\inttypes.h(69): error C2143: syntax error: missing ‘{‘ before ‘__cdecl‘
C:\Program Files (x86)\Windows Kits\10\include\10.0.10240.0\ucrt\inttypes.h(76): error C2143: syntax error: missing ‘{‘ before ‘__cdecl‘
C:\Program Files (x86)\Windows Kits\10\include\10.0.10240.0\ucrt\inttypes.h(82): error C2143: syntax error: missing ‘{‘ before ‘__cdecl‘
C:\Program Files (x86)\Windows Kits\10\include\10.0.10240.0\ucrt\inttypes.h(89): error C2143: syntax error: missing ‘{‘ before ‘__cdecl‘
C:\Program Files (x86)\Windows Kits\10\include\10.0.10240.0\ucrt\inttypes.h(95): error C2143: syntax error: missing ‘{‘ before ‘__cdecl‘
error: command ‘C:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\VC\\BIN\\x86_amd64\\cl.exe‘ failed with exit status 2
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
4.解決building ‘Crypto.Random.OSRNG.winrandom‘ extension
1.添加VC環境變量,設置用戶環境變量,這裏劃重點!!!是用戶環境變量,不是系統環境變量
變量名:VCINSTALLDIR
(變量值爲vs安裝路徑下的VC,默認是這個)
變量值:C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC
2. win+R運行cmd,執行命令set CL=/FI」%VCINSTALLDIR%\INCLUDE\stdint.h」 %CL%
3. 再次進入pycryto的解壓文件夾python setup.py install ,看到
能顯示running install_egg_info 就是安裝成功了
5.注意是Crypto仍是crypto,區分大小寫
我這裏安裝成功以後代碼alt+enter自動關聯的是
from crypto.Cipher import AES
1
而後代碼運行報錯,點進去AES裏面import的是
from Crypto.Cipher import _AES1小夥伴若是遇到跟我同樣的問題,能夠手動進入python\Lib下,個人路徑是 E:\Python\Python36\Lib 而後把文件夾crypto改爲Crypto。把小寫改爲大寫,刷新下,完美運行~~~~--------------------- 做者:孤影惆悵 來源:CSDN 原文:https://blog.csdn.net/a624806998/article/details/78596543 版權聲明:本文爲博主原創文章,轉載請附上博文連接!