V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
Hualin
V2EX  ›  Python

【存】给Python 安装并编译第三方模块的一点小折腾

  •  
  •   Hualin · 2012-05-17 22:42:06 +08:00 · 3907 次点击
    这是一个创建于 4332 天前的主题,其中的信息可能已经有所发展或是发生改变。
    安装第三方模块的方法很多,python文档中标准的方法是用 distutils并结合setup.py完成。还有一种方法是我在安装IPython的时候看到的,安装distribute,然后使用easy_install直接从PyPI远程下载安装。

    昨天下午就是这么折腾把IPython和它的各种dependencies安装上咯。我的系统是win7,编译器使用mingw32.

    之后我需要使用一个叫做svmlight的科学计算模块。开始我使用easy_install但是不起作用。也不知到底发生了什么。

    后来我下载了源代码尝试使用distutils安装,遇到如下问题:
    0.> setup.py install
    系统反馈 无法移除某个需要移除的C文件,
    svmlight的setup.py如下:

    lib_sources = glob('lib/*.c')
    print lib_sources ##我加的
    lib_sources.remove('lib/svm_loqo.c') # this is an alternate backend for SVM-Light; only
    # one of {svm_loqo.c,svm_hideo.c} may be compiled
    # with this extension.
    lib_sources.remove('lib/svm_classify.c') # this file implements the "classify" binary;
    # don't include it, since it defines main()
    # again!

    后来发现这中目录格式完全是为unix写的,必须换成\\,
    所以源代码改成:
    lib_sources.remove('lib\\svm_loqo.c') # this is an alternate backend for SVM-Light; only
    # one of {svm_loqo.c,svm_hideo.c} may be compiled
    # with this extension.
    lib_sources.remove('lib\\svm_classify.c') # this file implements the "classify" binary;
    # don't include it, since it defines main()
    # again!

    然后再次运行,就通过了;

    1.> setup.py install
    在build过程中 系统反馈 无法找到 vcvarsall.bat文件,后来知道这是msvs编译器的东西;
    于是尝试指定 -compiler = mingw32
    2. > setup.py install -compiler = mingw32
    系统会跳过检查;

    3. > setup.py install -compiler = mingw32
    系统又会跳出来一个错误:
    C:\MinGW\bin\gcc.exe -mno-cygwin -mdll -O -Wall -Ilib/ -IC:\Python27\include -IC:\Python27\PC -c svmlight/svmlight.c -o build\temp.win32-2.7\Release\svmlight\svmlight.o
    cc1.exe: error: unrecognized command line option '-mno-cygwin'
    error: command 'gcc' failed with exit status 1

    这是python模块distutils中 cygwinccompiler.py的bug。其实也不算是bug。因为gcc在新版本中把-mno-cygwin移除了,所以就无法找到了。或者换就一点的mingw32或者改cygwinccompiler.py。
    后者的方法是:打开源文件,把 -mno-cygwin相关的字段全部删除。代码如下:

    # self.set_executables(compiler='gcc -mno-cygwin -O -Wall',
    # compiler_so='gcc -mno-cygwin -mdll -O -Wall',
    # compiler_cxx='g++ -mno-cygwin -O -Wall',
    # linker_exe='gcc -mno-cygwin',
    # linker_so='%s -mno-cygwin %s %s'
    # % (self.linker_dll, shared_option,
    # entry_point))
    改为:
    self.set_executables(compiler='gcc -O -Wall',
    compiler_so='gcc -mdll -O -Wall',
    compiler_cxx='g++ -O -Wall',
    linker_exe='gcc',
    linker_so='%s %s %s'
    % (self.linker_dll, shared_option,
    entry_point))
    其实我还是觉得很不保险,不过就是尝试一下,后来在执行,居然将这个模块编译进去了。

    一点废话:还是搞不懂为啥easy_install为啥没法安装svmlight,对python编译第三方模块也不是完全了解。另外Windows下写代码真悲催,很多黑客写的模块都把地址写死了,真心希望能用unix,其实我只是觉得ubuntu太难看,而实验室不给配mac罢了。
    1 条回复    1970-01-01 08:00:00 +08:00
    bhuztez
        1
    bhuztez  
       2012-05-17 23:03:25 +08:00
    Fedora 吧
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3338 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 13:23 · PVG 21:23 · LAX 06:23 · JFK 09:23
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.