无法创建带有多平台编译的外部模块的setup.py文件

2024-09-28 19:21:33 发布

您现在位置:Python中文网/ 问答频道 /正文

我想用一个平台不可知论者来运行cx\u freeze设置.py我不知道如何将编译后的文件(.pym,.so)添加到可执行文件中

DataProcessor是Cython在外部编译的python模块。。。但是我不知道如何在cx\u中包含它,因为绝对路径取决于plataform和python版本。那我该怎么处理呢。你知道吗

编译了可执行文件,但没有包含外部模块,因此当我运行应用程序时,会抛出一个错误,即没有加载DLL,或者在MacOS中显示ModuleNotFoundError: No module named 'DataProcessor'

编辑:我在原稿上看到一个错误设置.py,更正此错误cx\u freeze显示此错误

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

EDIT2:根据@mgracer的建议,他试图放入includes部分,但没有成功的cx\u冻结显示

ImportError: No module named 'DataProcessor'

我该怎么做才能保持我的健康呢设置.pyplataform不可知论者。你知道吗

这就是我现在所拥有的

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

Tags: installpathpyimportbaseoswindowspackages
1条回答
网友
1楼 · 发布于 2024-09-28 19:21:33

我把问题解决了设置.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)]
)

相关问题 更多 >