我正在尝试使用与plataform无关的setup.py文件运行cx_freeze,我无法想象如何将已编译的文件(.pym,.so)添加到可执行文件中
DataProcessor是一个由Cython外部编译的python模块...但我不知道如何将它包含在cx_freeze可执行文件中,因为绝对路径依赖于plataform和python版本。那我该怎么办呢。
编译可执行文件,但是不包含外部模块,所以当我运行应用程序时会抛出一个错误,表明没有加载DLL或者MacOS说 ModuleNotFoundError: No module named 'DataProcessor'
编辑:我在原始setup.py上看到错误,更正此错误cx_freeze显示此错误
cx_Freeze.freezer.ConfigError:找不到名为DataProcessor的文件/目录
EDIT2:正如@mgracer所建议的那样试图放入包含部分但没有成功的cx_freeze显示
ImportError:没有名为'DataProcessor'的模块
所以我能做些什么来维护我的setup.py plataform-agnostic。
这就是我现在所拥有的
from Cython.Build import cythonize
from cx_Freeze import setup, Executable
import sys
import os.path
# Windows hack
PYTHON_INSTALL_DIR = os.path.dirname(os.path.dirname(os.__file__))
os.environ['TCL_LIBRARY'] = os.path.join(PYTHON_INSTALL_DIR, 'tcl', 'tcl8.6')
os.environ['TK_LIBRARY'] = os.path.join(PYTHON_INSTALL_DIR, 'tcl', 'tk8.6')
# Windows hack
includes = []
excludes = ['tkinter']
packages = ['openpyxl', 'sqlite3', 're', 'collections', 'os']
include_files = ['DataProcessor']
dll_excludes = []
# GUI applications require a different base on Windows (the default is for a
# console application).
base = None
if sys.platform == "win32":
base = "Win32GUI"
# Dependencies are automatically detected, but it might need fine tuning.
build_exe_options = {
"excludes": excludes,
"includes": includes,
"packages": packages,
"include_files": include_files
}
setup(
name="analizador",
version="0.1",
description="Foo bar",
options={"build_exe": build_exe_options},
ext_modules=cythonize("DataProcessor.pyx"),
executables=[Executable("analisis.py", base=base)]
)
答案 0 :(得分:1)
我解决了它扩展了一点setup.py脚本 结果如下:
from Cython.Build import cythonize
from cx_Freeze import setup, Executable
import sys
import os
import platform
# hack para correr en windows
PYTHON_INSTALL_DIR = os.path.dirname(os.path.dirname(os.__file__))
os.environ['TCL_LIBRARY'] = os.path.join(PYTHON_INSTALL_DIR, 'tcl', 'tcl8.6')
os.environ['TK_LIBRARY'] = os.path.join(PYTHON_INSTALL_DIR, 'tcl', 'tk8.6')
# hack
setup(ext_modules=cythonize("DataProcessor.pyx"))
# rutina detect the files
arch = platform.machine()
temp = platform.python_version_tuple()
pyver = '%s.%s' % (temp[0], temp[1])
pname = None
pext = ".so"
tfiles = ()
if sys.platform == 'darwin':
temp = platform.mac_ver()
tver = '.'.join(temp[0].split('.')[:2])
ptemp = 'macosx'
pname = '%s-%s-%s' % (ptemp, tver, temp[2])
if sys.platform == 'win32':
ptemp = 'win'
pname = '%s-%s' % (ptemp, arch.lower())
pext = '.pym'
libpath = os.path.join('build', ('lib.%s-%s' % (pname, pyver)))
afiles = os.listdir(libpath)
for file in afiles:
afile = file.split('.')
tfiles = tfiles + ((os.path.join(libpath, file), '.'.join([afile[0],
afile[2]])),)
# end
includes = []
excludes = ['tkinter', 'PyQt4']
packages = ['openpyxl', 'sqlite3', 're', 'collections', 'os']
dll_excludes = []
# GUI applications require a different base on Windows (the default is for a
# console application).
base = None
if sys.platform == "win32":
base = "Win32GUI"
# Dependencies are automatically detected, but it might need fine tuning.
build_exe_options = {
"excludes": excludes,
"includes": includes,
"packages": packages,
"include_files": tfiles
}
setup(
name="foo",
version="0.1",
description="Foobar",
options={"build_exe": build_exe_options},
executables=[Executable("analisis.py", base=base)]
)