在Windows下构建FANN Python绑定
我尝试通过 python setup.py install 来编译 FANN 的 Python 绑定(叫 pyfann)。我在安装了 Visual Studio 2010 和 SWIGWIN 的情况下遇到了几个错误。
有没有人能提供一个已经编译好的 Python 绑定版本,适用于 Windows 7 和/或 8 的 Python 2.7?我也试过这个 在 Windows 下的 pyfann,但是 _libfann.pyd 的构建失败了。
1 个回答
2
我已经为Python 2.7编译了fann。
你可以从这里下载: https://github.com/jacekm-git/pyfann_python2.7_windows
编译步骤:
1. 安装Swig:
- 下载地址:sourceforge.net/projects/swig/files/swigwin/
- 把下载的文件解压到C:\swigwin\
- 编辑系统路径变量:
- 右键点击“我的电脑” -> 选择“属性”
- 点击“高级”选项卡 -> 选择“环境变量”
- 编辑“Path”变量 -> 在行末添加";C:\swigwin\;C:\python27\;C:\python27\Scripts\"。
2. 安装Microsoft Visual C++ Studio 2008 Express:
- 下载并安装:去这里:go.microsoft.com/?linkid=7729279
3. 下载Fann 2.1
- 下载地址: http://sourceforge.net/projects/fann/files/fann/2.1.0beta/fann-2.1.0beta.zip/download
- 把下载的文件解压到:C:\fann\
4. 编译fanndoubled.dll
- 在Visual C++ Studio 2008中打开C:\fann\MicrosoftWindowsDll\windll.vcproj
- 选择“Build” -> 点击“Build windll”
- 把fanndoubled.dll和fanndoubled.lib文件复制到C:\fann\python\pyfann
5. 使用Swig
- 打开命令提示符
cd c:\fann\python\pyfann\
swig -c++ -python pyfann.i
- 在Visual C++ Studio 2008中打开C:\fann\python\pyfann.wrap.cxx
- 找到这一行(按ctrl+f):SWIGINTERN PyObject *_wrap_training_data_parent_create_train_from_callback
SWIGINTERN PyObject *_wrap_training_data_parent_create_train_from_callback(PyObject*SWIGUNUSEDPARM(self), PyObject *args) {
PyObject *resultobj = 0;
FANN::training_data *arg1 = (FANN::training_data *) 0 ;
unsigned int arg2 ;
unsigned int arg3 ;
unsigned int arg4 ;
void (*arg5)(unsigned int,unsigned int,unsigned int,fann_type *,fann_type *) = (void (*)(unsigned int,unsigned int,unsigned int,fann_type *,fann_type *)) 0 ;
// .... rest of file ...
改成:
typedef void (__stdcall *arg5_fn)(unsigned int, unsigned int, unsigned int, fann_type*, fann_type*); // added line
SWIGINTERN PyObject *_wrap_training_data_parent_create_train_from_callback(PyObject *SWIGUNUSEDPARM(self), PyObject *args) {
PyObject *resultobj = 0;
FANN::training_data *arg1 = (FANN::training_data *) 0 ;
unsigned int arg2 ;
unsigned int arg3 ;
unsigned int arg4 ;
/* void (*arg5)(unsigned int,unsigned int,unsigned int,fann_type *,fann_type *) = (void (*)(unsigned int,unsigned int,unsigned int,fann_type *,fann_type *)) 0 ;*/
arg5_fn arg5= (arg5_fn)NULL; // added line
// ....
6. 编辑setup.py
- 在编辑器中打开C:\fann\python\pyfann\setup.py
- 找到并注释掉以下行:
# swig_cmd = 'swig -c++ -python pyfann/pyfann.i'
# print 'Running SWIG before:', swig_cmd
# os.system(swig_cmd)
- 把setup的行改成:
ext_modules=[Extension('pyfann._libfann',['pyfann/pyfann_wrap.cxx'],
include_dirs=['../src/include'],
# extra_objects=['../src/doublefann.o'],
libraries = ['pyfann/fanndoubled'],
define_macros=[("SWIG_COMPILE",None)]
),
7. 安装
cd C:\fann\python\
python setup.py install
copy C:\fann\python\pyfann\fanndoubled.dll C:\Python27\Lib\site-packages\pyfann\