1. python安装某些第三方包报错vcvarsall.bat

原因:python安装某些第三方包(这里主要针对Windows平台),主要是涉及系统底层和本地代码库时,需要C编译环境,Windows平台下,python本身是通过VC的编译环境编译的,不同的python版本对应不同的VC的动态库,同时,第三方库可能是采用其他编译器(比如GNU C、Cygwin、MinGW等)编译扩展的C代码。

Mingw官方链接:http://sourceforge.net/projects/mingw/

官方说明:python3的安装包的构建主要是通过内置的Distutils工具来完成(https://docs.python.org/3.4/install/)

Whenever possible, the Distutils try to use the configuration information made available by the Python 
interpreter used to run the setup.py script. For example, the same compiler and linker flags used to compile 
Python will also be used for compiling extensions. Usually this will work well, but in complicated situations 
this might be inappropriate. This section discusses how to override the usual Distutils behaviour.

解决方案:

  1. 安装python对应版本的VC编译器,可以直接安装整个visual studio;
  2. 如果不想整个安装VC,则可以选择安装MinGW(并将其安装后的bin等目录加入到系统path),然后配置python分发包的默认编译工具,
    即在python安装目录的Lib\distutils目录下(比如安装的python3.4版本,那么即似C:\Python34\Lib\distutils),新建distutils.cfg文件(如果已有,则直接编辑之)。打开该文件,在前面添加如下内容(这里是32位的mingw,所以是mingw32,64位则为mingw64)。

    [build]
    compiler=mingw32
    

    上述设置之后python distutils构建带C扩展的库或者安装C扩展的库之时,全部都切换至设置的编译器,如果只是想修改某一次的构建或者安装时使用的编译器,则可以在构建或者安装的时候加上参数,如:

    python setup.py build --compiler=mingw32 install
    
  3. 如果也不想自己手动安装mingw,想更简便,有一种便捷的方式可以走。一般情况下,大家都会在自己的windows主机上安装Git(这里安装的是64位的git,如果安装32位的git,那么对应的是mingw32),下载的官方地址为windows git官方下载,安装之后,会自动安装mingw(windows版本Git依赖mingw,安装在git的安装目录下),如下图所示:

    此时将mingw的bin目录也就是D:\Program Files\Git\mingw64\bin添加到系统path环境变量中。然后就和方法2中的后续步骤一致,即配置python分发包的默认编译工具,在python安装目录的Lib\distutils目录下(比如安装的python3.4版本,那么即似C:\Python34\Lib\distutils),新建distutils.cfg文件(如果已有,则直接编辑之)。打开该文件,在前面添加如下内容(这里是64位的mingw,所以是mingw64,32位则为mingw32)。

    [build]
    compiler=mingw64
    

采用上述方法配置好之后,再次安装即可成功。

2.安装某些包时报错,类似”collect2.exe: error: ld returned 1 exit status”,

C:\Python27\libs/libpython27.a(dmmes01026.o):(.idata$7+0x0): undefined reference to `_head_C__build27_cpython_PCBuild_libpython27_a'
C:\Python27\libs/libpython27.a(dmmes00281.o):(.idata$7+0x0): undefined reference to `_head_C__build27_cpython_PCBuild_libpython27_a'
C:\Python27\libs/libpython27.a(dmmes00105.o):(.idata$7+0x0): undefined reference to `_head_C__build27_cpython_PCBuild_libpython27_a'
C:\Python27\libs/libpython27.a(dmmes00253.o):(.idata$7+0x0): undefined reference to `_head_C__build27_cpython_PCBuild_libpython27_a'
C:\Python27\libs/libpython27.a(dmmes00227.o):(.idata$7+0x0): undefined reference to `_head_C__build27_cpython_PCBuild_libpython27_a'
C:\Python27\libs/libpython27.a(dmmes00712.o):(.idata$7+0x0): more undefined references to `_head_C__build27_cpython_PCBuild_libpython27_a' follow
collect2.exe: error: ld returned 1 exit status

原因:和问题1本质一样,由于Windows平台下的python编译器本身由VC环境编译,其生成的pythonxx.dll(xx是版本号,比如27、34)和pythonxx.lib等可能与需要安装的第三方库(采用Cygwin、MinGW等编译器(环境)生成并发布)无法兼容.

解决方案:
To create Python extensions, you need to link against the Python library. Unfortunately, most Python distributions are provided with Pythonxx.lib, a library in Microsoft Visual C++ format. GCC expects a.a file (libpythonxx.a to be precise.). Here’s how to convert pythonxx.lib to libpythonxx.a:

a.Download pexport (from here or http://sourceforge.net/projects/mingw/files/MinGW/Extension/pexports/).

b.Get pythonxx.dll (it should be somewhere on your harddrive).

c.Run : pexports pythonxx.dll > pythonxx.def This will extract all symbols from pythonxx.dll and write them into pythonxx.def.

d.Run : dlltool –dllname pythonxx.dll –def pythonxx.def –output-lib libpythonxx.a This will create libpythonxx.a (dlltool is part of MinGW utilities).
Copy libpythonxx.a to c:\pythonxx\libs\ (in the same directory as pythonxx.lib).
This trick should work for all Python versions, including future releases of Python. You can also use this trick to convert other libraries.