我们使用Python做加密算法如AES、MD5、SHA等时,需要用到PyCrypto模块

PyCrypto模块的安装方法

1、一般在官方网站下载:

https://www.dlitz.net/software/pycrypto/
然后使用命令就可以安装成功了:
python setup.py build
python setup.py install
2、如果在windows下会报错:
  Python error: Unable to find vcvarsall.bat
 

在安装一些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/

 
http://www.cnblogs.com/lazyboy/p/4017567.html
 

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."
    = sys.version.find(prefix)
    if == -1:
        return 6
    = + 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

python 加密模块安装的更多相关文章

  1. Python的安装和详细配置

    Python是一种面向对象.解释型计算机程序设计语言.被认为是比较好的胶水语言.至于其他的,你可以去百度一下.本文仅介绍python的安装和配置,供刚入门的朋友快速搭建自己的学习和开发环境.本人欢迎大 ...

  2. python requests 安装

    在 windows 系统下,只需要输入命令 pip install requests ,即可安装. 在 linux 系统下,只需要输入命令 sudo  pip install requests ,即可 ...

  3. Python 的安装与配置(Windows)

    Python2.7安装配置 python的官网地址:https://www.python.org/ 我这里下载的是python2.7.12版本的 下载后点击安装文件,直接点击下一步知道finally完 ...

  4. 初学python之安装Jupyter notebook

    一开始安装python的时候,安装的是最新版的python3.6的最新版.而且怕出问题,选择的都是默认安装路径.以为这样总不会出什么问题.一开始确实这样,安装modgodb等一切顺利.然而在安装jup ...

  5. 转: python如何安装pip和easy_installer工具

    原文地址: http://blog.chinaunix.net/uid-12014716-id-3859827.html 1.在以下地址下载最新的PIP安装文件:http://pypi.python. ...

  6. CentOS 6.5升级Python和安装IPython

    <转自:http://www.noanylove.com/2014/10/centos-6-5-sheng-ji-python-he-an-zhuang-ipython/>自己常用.以做备 ...

  7. python Scrapy安装和介绍

    python Scrapy安装和介绍 Windows7下安装1.执行easy_install Scrapy Centos6.5下安装 1.库文件安装yum install libxslt-devel ...

  8. window下从python开始安装科学计算环境

    Numpy等Python科学计算包的安装与配置 参考: 1.下载并安装 http://www.jb51.net/article/61810.htm 1.安装easy_install,就是为了我们安装第 ...

  9. Python virtualenv安装库报错SSL: CERTIFICATE_VERIFY_FAILED

    Python virtualenv安装库报错SSL: CERTIFICATE_VERIFY_FAILED 问题描述 使用pip按照virtualenv报错,如下: pip install virtua ...

随机推荐

  1. iOS触摸事件深度解析-备用

    概述 本文主要解析从我们的手指触摸苹果设备到最终响应事件的整个处理机制.本质上讲,整个过程可以分为两个步骤: 步骤1:找目标.在iOS视图层次结构中找到触摸事件的最终接受者: 步骤2:事件响应.基于i ...

  2. Java基础语法学习(1)switch...case

    switch...case的标准语法 switch(待选择的变量) { case 值1:语句1; break; case 值2:语句2: break; ....... case 值n:语句n; bre ...

  3. 防止DC电源反接的方法——SS14的用法

    出处:http://blog.ednchina.com/tengjingshu 电源是PCB板的重要部分,每个芯片都需要电源供给.芯片其实是挺脆弱的,只要正负接反得话,大多数就会挂掉,相信很多人都有惨 ...

  4. DateTime字段控件值显示短格式的做法

    后台取dateTime格式,前台格式化就好了 <input type="text" name="txtPartyTime" id="txtPar ...

  5. 使IE6同样支持圆角效果

    之前写到过,IE6不支持:hover效果的解决办法,其它这个跟它一样.IE6(7/8)不支持border-radius属性,所以其中的圆角效果显示不出来,可以通过引用ie-css3.htc的方法解决. ...

  6. Java Access Levels(访问控制)

    Access Levels Modifier Class Package Subclass World public Y Y Y Y protected Y Y Y N no modifier Y Y ...

  7. 【译】typeof null的前世今生

        更新时间2013-11-05:为了更好的解释为什么typeof null的结果是object,我看了一下C代码的实现(译者注:Javascript源码).       在Javascript语 ...

  8. NUnit - 使用感受

    Nunit使用 最近项目开始大量使用Nunit, 发现Nunit还是有很多好处的. 1. 测试驱动逻辑,这样可以尽最大可能减少“修改”引入的Bug. 如果你修改了一些东西,导致Case跑不过.请检查你 ...

  9. jQuery 遍历 json 方法大全

    1.for循环: var obj = { "status":1, "bkmsg":"\u6210\u529f", "bkdata& ...

  10. JSP中getParameter和getAttribute区别

    (1)HttpServletRequest类有setAttribute()方法,而没有setParameter()方法 (2)当两个Web组件之间为链接关系时,被链接的组件通过getParameter ...