zoukankan      html  css  js  c++  java
  • python 加密模块安装

     我们使用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安装目录>Libdistutilsmsvc9compiler.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:UsersAdministratorAppDataLocalProgramsCommonMicrosoftVisual C++ for Python9.0

    2.修改python安装目录下Libdistutilsmsvc9compiler.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:UsersAdministratorAppDataLocalProgramsCommonMicrosoftVisual C++ for Python9.0vcvarsall.bat'
        vsbase = VS_BASE % version
        try:
            productdir = Reg.get_value(r"%sSetupVC" % vsbase,
                                       "productdir")
        except KeyError:
            productdir = None
     
        # trying Express edition
        if productdir is None:
            vsbase = VSEXPRESS_BASE % version
            try:
                productdir = Reg.get_value(r"%sSetupVC" % 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
  • 相关阅读:
    识别验证码
    Jquery利用ajax调用asp.net webservice的各种数据类型(总结篇)
    Intellij IDEA新建maven项目和配置tomcat
    Intellij IDEA 14使用maven3.3.3 问题
    使用Xshell连接Ubuntu
    IntelliJ IDEA 12 详细开发教程(二)Tomcat服务配置与Jrebel热部署
    git使用
    e.keycode详解
    jquery插件制作教程 txtHover(转载)
    深入理解JavaScript系列(转载)
  • 原文地址:https://www.cnblogs.com/yaks/p/6134092.html
Copyright © 2011-2022 走看看