最近接觸公司后台管理系統的開發,其中涉及到加密模塊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
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 _AES
- 1
小伙伴如果遇到跟我一樣的問題,可以手動進入python\Lib下,我的路徑是
E:\Python\Python36\Lib
然后把文件夾crypto改成Crypto。把小寫改成大寫,刷新下,完美運行~~~~