使用 py2exe 构建带有扩展的 python 程序
题
我很难找到 py2exe 配方,特别是对于需要 c 扩展的情况。
以下配方在没有“ext_modules”部分的情况下也可以正常工作。有了它我得到“NameError:名称“扩展”未定义。
from distutils.core import setup
import py2exe
import matplotlib
import os
s = os.popen('svnversion')
version = s.read()
f = open('cpa_version.py', 'w')
f.write('VERSION = "%s"\n'%(version.strip()))
f.close()
setup(console=['cpa.py'],
options={
'py2exe': {
'packages' : ['matplotlib', 'pytz', 'MySQLdb', 'pysqlite2'],
'includes' : ['PILfix', 'version'],
"excludes" : ['_gtkagg', '_tkagg',
"Tkconstants","Tkinter","tcl"],
"dll_excludes": ['libgdk-win32-2.0-0.dll',
'libgobject-2.0-0.dll',
'libgdk_pixbuf-2.0-0.dll',
'tcl84.dll', 'tk84.dll']
}
},
data_files=matplotlib.get_py2exe_datafiles(),
# how to build _classifier.c???
ext_modules = [Extension('_classifier',
sources = ['_classifier.c'],
include_dirs=[numpy.get_include()],
libraries = ['sqlite3'])]
)
_classifier.c包含以下内容
#include "sqlite3.h"
#include "Python.h"
#include "numpy/arrayobject.h"
#include <stdio.h>
任何帮助将不胜感激。
解决方案
修复了因忘记导入扩展而产生的小错误后,我遇到了其他错误,指出 -lsqlite3 标志存在问题。事实证明我需要遵循此处概述的步骤: http://cboard.cprogramming.com/cplusplus-programming/82135-sqlite-questions.html
- 从 sqlite.org/download.html 下载 sqlitedll-3_3_7.zip 和 sqlite-source-3_3_7.zip
解压 sqlitedll-3.3.7.zip,然后从命令行运行:
dlltool -D sqlite3.dll -d sqlite3.def -l libsqlite3dll.a
- 将 libsqlite3dll.a(刚刚创建的)放在 MinGW lib 目录中。
- 将 sqlite3.dll 放在系统路径中(c:\Windows\System32\ 对我有用)
- 解压 sqlite-source-3_3_7.zip 并将 sqlite3.h 放入 MinGW 包含目录中。
- 链接时,您需要提供参数:-lsqlite3dll(这意味着将libraries=['sqlite3']更改为libraries=['sqlite3dll'])
...之后构建就成功了。
这是设置文件:
from distutils.core import setup, Extension
import py2exe
import matplotlib
import os
import numpy
setup(console=['cpa.py'],
options={
'py2exe': {
'packages' : ['matplotlib', 'pytz', 'MySQLdb', 'pysqlite2'],
'includes' : ['PILfix', 'version'],
"excludes" : ['_gtkagg', '_tkagg',
"Tkconstants","Tkinter","tcl"],
"dll_excludes": ['libgdk-win32-2.0-0.dll',
'libgobject-2.0-0.dll',
'libgdk_pixbuf-2.0-0.dll',
'tcl84.dll', 'tk84.dll']
}
},
data_files=matplotlib.get_py2exe_datafiles(),
ext_modules = [Extension('_classifier',
sources = ['_classifier.c'],
include_dirs=[numpy.get_include()],
libraries = ['sqlite3dll'])]
)
其他提示
尝试改变
from distutils.core import setup
到
from distutils.core import setup, Extension
不隶属于 StackOverflow