gpt4 book ai didi

python - 使用 cx_Freeze 从导入 Pandas 的 Python3.6 脚本构建可执行文件时出错

转载 作者:太空宇宙 更新时间:2023-11-03 11:39:37 27 4
gpt4 key购买 nike

我正在尝试使用包含 pandas 和 numpy 的 cx_Freeze 创建 Python 3.6 可执行文件。我正在使用 Python 3.6.5 和使用 virtualenvwrapper 创建的虚拟环境。我在 Windows 10 上开发。cx_Freeze 版本是 5.1。 Pandas 版本是 0.23.4。

我的 setup.py 看起来像这样:

import os
from cx_Freeze import setup, Executable

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

executables = [Executable("main.py", base=base)]

packages = ["idna", "os", "numpy","importlib", "pandas"]
options = {
'build_exe': {
'packages':packages,
'include_files':[
os.path.join(PYTHON_INSTALL_DIR, 'DLLs', 'tk86t.dll'),
os.path.join(PYTHON_INSTALL_DIR, 'DLLs', 'tcl86t.dll'),
os.path.join(PYTHON_INSTALL_DIR, 'DLLs', 'sqlite3.dll'),
],
},
}

setup(
name = "MyScript",
options = options,
version = "0.1",
description = 'Placeholder desc',
executables = executables
)

请注意,我手动设置了 env 变量,因为在构建过程中未找到它们,我不得不手动将 DLL 移动到 virtualenv 文件夹。

脚本看起来像这样:

import numpy as np
import pandas as pd
import tkinter as tk
root = tk.Tk()
root.mainloop()

如果我注释掉 import pandas as pd,一切正常。如果我添加 pandas 导入,我会收到以下错误:

C:\path\to\project\build\exe.win-amd64-3.6>MyScript.exe
Traceback (most recent call last):
File "C:\path\to\Envs\MyEnv\lib\site-packages\cx_Freeze\initscripts\__startup__.py", line 14, in run
module.run()
File "C:\path\to\Envs\MyEnv\lib\site-packages\cx_Freeze\initscripts\Console.py", line 26, in run
exec(code, m.__dict__)
File "MyScript.py", line 2, in <module>
File "C:\path\to\Envs\MyEnv\lib\site-packages\pandas\__init__.py", line 23, in <module>
from pandas.compat.numpy import *
File "C:\path\to\Envs\MyEnv\lib\site-packages\pandas\compat\__init__.py", line 32, in <module>
from distutils.version import LooseVersion
File "C:\path\to\Envs\MyEnv\lib\distutils\__init__.py", line 17, in <module>
real_distutils = imp.load_module("_virtualenv_distutils", None, distutils_path, ('', '', imp.PKG_DIRECTORY))
File "C:\path\to\Envs\MyEnv\lib\imp.py", line 245, in load_module
return load_package(name, filename)
File "C:\path\to\Envs\MyEnv\lib\imp.py", line 217, in load_package
return _load(spec)
File "<frozen importlib._bootstrap>", line 683, in _load
AttributeError: 'NoneType' object has no attribute 'name'

我在网上看到了很多论坛和堆栈溢出问题(实际上我必须修复很多错误才能到达这里)但是我无法弄清楚 cx_Freeze 缺少什么。

我卸载并重新安装了 pandas,我卸载了 cx_Freeze 并尝试安装以前的版本(安装失败但这是一个不同的 SO 问题)。我实际上尝试了 pyinstaller 和 py2exe,但有太多错误,这些错误不那么冗长(更难修复)以至于我放弃了除了 cx_Freeze 之外的任何东西来创建可执行文件。

有人可以帮忙吗?

最佳答案

cx_Freeze 版本 5.1.1 中,包含的模块位于构建目录的子目录 lib 中。手动添加的 DLL 显然也需要移动到那里。

您可以通过对 setup.py 脚本进行以下修改来执行此操作:

options = {
'build_exe': {
'packages': packages,
'include_files': [
(os.path.join(PYTHON_INSTALL_DIR, 'DLLs', 'tk86t.dll'), os.path.join('lib', 'tk86t.dll')),
(os.path.join(PYTHON_INSTALL_DIR, 'DLLs', 'tcl86t.dll'), os.path.join('lib', 'tcl86t.dll')),
(os.path.join(PYTHON_INSTALL_DIR, 'DLLs', 'sqlite3.dll'), os.path.join('lib', 'sqlite3.dll'))
],
},
}

我实际上不确定sqlite3.dll是否也需要移动到lib

您的问题很有趣,因为它似乎表明 pandastkinter 之间存在导入冲突。这大概就是为什么您收到与此问题中报告的错误消息不同的原因:
Getting "ImportError: DLL load failed: The specified module could not be found" when using cx_Freeze even with tcl86t.dll and tk86t.dll added in

编辑:我使用以下配置在 Windows 7 上使用 Python 3.6.5 设法卡住并运行 OP 的示例脚本 main.py 而没有错误

idna 2.7 (installed with pip)
numpy 1.14.3+mkl (installed using Gohlke's binaries)
pandas 0.23.4 (installed with pip)
cx_Freeze 5.1.1 (installed with pip)

我使用以下 setup.py 脚本:

import os
import sys
from cx_Freeze import setup, Executable

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

base = None
if sys.platform == "win32":
base = "Win32GUI"

executables = [Executable("main.py", base=base)]

packages = ["idna", "os", "numpy", "importlib", "pandas"]
options = {
'build_exe': {
'packages': packages,
'include_files': [
(os.path.join(PYTHON_INSTALL_DIR, 'DLLs', 'tk86t.dll'), os.path.join('lib', 'tk86t.dll')),
(os.path.join(PYTHON_INSTALL_DIR, 'DLLs', 'tcl86t.dll'), os.path.join('lib', 'tcl86t.dll')),
(os.path.join(PYTHON_INSTALL_DIR, 'DLLs', 'sqlite3.dll'), os.path.join('lib', 'sqlite3.dll'))
],
},
}

setup(
name="MyScript",
options=options,
version="0.1",
description='Placeholder desc',
executables=executables
)

关于python - 使用 cx_Freeze 从导入 Pandas 的 Python3.6 脚本构建可执行文件时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52248985/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com