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

2012-05-17 22:42:06 +08:00
 Hualin
安装第三方模块的方法很多,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罢了。
3929 次点击
所在节点    Python
1 条回复
bhuztez
2012-05-17 23:03:25 +08:00
Fedora 吧

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/36384

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX