我正在尝试使用cx_Freeze创建一个Python 3.6可执行文件,其中包括熊猫和numpy。我正在使用Python 3.6.5和使用virtualenvwrapper创建的虚拟环境。我在Windows 10上开发。cx_Freeze版本是5.1。熊猫版本是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
,一切正常。如果添加熊猫导入,则会出现以下错误:
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并尝试安装以前的版本(安装失败,但这是一个不同的问题)。我实际上尝试了pyinstaller和py2exe,但是有太多错误,这些错误不那么冗长(难以修复),因此我放弃了使用cx_Freeze以外的任何方法来创建可执行文件。
有人可以帮忙吗?
答案 0 :(得分:1)
在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
。
鉴于该问题似乎表明pandas
与tkinter
之间存在导入冲突,因此您的问题很有趣。这可能就是为什么您收到与该问题报告的错误消息不同的原因的原因:
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(与pip一起安装)
numpy 1.14.3 + mkl(使用Gohlke's binaries安装)
熊猫0.23.4(已安装pip)
cx_Freeze 5.1.1(随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
)