无法使用多平台编译外部模块制作 setup.py 文件

Can't make a setup.py file with multiplataform-compiled external modules

我正在尝试 运行 cx_freeze 使用与平台无关的 setup.py 文件,我不知道如何添加已编译的文件(.pym、.so)进入可执行文件

DataProcessor 是一个由 Cython 外部编译的 python 模块...但我不知道如何将它包含在 cx_freeze 可执行文件中,因为绝对路径取决于 plataform 和 python 版本。那么我该如何处理呢。

可执行文件已编译但不包含外部模块,因此当我 运行 应用程序抛出 DLL 未加载的错误或在 MacOS 的情况下显示 ModuleNotFoundError: No module named 'DataProcessor'

编辑:我在原始 setup.py 上看到一个错误,更正此错误 cx_freeze 显示此错误

cx_Freeze.freezer.ConfigError: cannot find file/directory named DataProcessor

EDIT2:正如@mgracer 所建议的,尝试放入包含部分但没有成功 cx_freeze 显示

ImportError: No module named 'DataProcessor'

所以我能做些什么来保持我的 setup.py 平台不可知论。

这就是我目前所拥有的

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)]
)

我解决了扩展 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)]
)