我正在使用 cx-Freeze 将 py 文件 (pm_artwork.py) 编译为 exe 文件。最初我使用的是:
Python 3.10.4、numpy 1.25.1、pandas 2.0.3、vs code。
在编译运行期间(与代码),程序能够产生预期的输出。
但是冻结成exe文件后,我想运行exe文件时出现这个错误(无论是使用cmd还是手动点击)
Traceback (most recent call last):
File "C:\Users\myName\AppData\Local\Programs\Python\Python310\Lib\site-packages\cx_Freeze\initscripts\__startup__.py", line 124, in run
module_init.run(name + "__main__")
File "C:\Users\myName\AppData\Local\Programs\Python\Python310\Lib\site-packages\cx_Freeze\initscripts\console.py", line 16, in run
exec(code, module_main.__dict__)
File "pm_artwork.py", line 6, in <module>
File "C:\Users\myName\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\__init__.py", line 11, in <module>
__import__(_dependency)
File "C:\Users\myName\AppData\Local\Programs\Python\Python310\lib\site-packages\numpy\__init__.py", line 141, in <module>
from . import core
File "C:\Users\myName\AppData\Local\Programs\Python\Python310\lib\site-packages\numpy\core\__init__.py", line 23, in <module>
from . import multiarray
File "C:\Users\myName\AppData\Local\Programs\Python\Python310\lib\site-packages\numpy\core\multiarray.py", line 85, in <module>
def empty_like(prototype, dtype=None, order=None, subok=None, shape=None):
File "C:\Users\myName\AppData\Local\Programs\Python\Python310\lib\site-packages\numpy\core\overrides.py", line 221, in decorator
return array_function_dispatch(
File "C:\Users\myName\AppData\Local\Programs\Python\Python310\lib\site-packages\numpy\core\overrides.py", line 175, in decorator
add_docstring(implementation, dispatcher.__doc__)
TypeError: add_docstring() argument 2 must be str, not None
我相信这与 numpy 版本有关。我做了进一步的研究,有人说他们尝试了低于 1.16 的 numpy 版本。
仍然使用Python 3.10.4,我尝试安装numpy 1.15.4,但出现此错误:
note: This error originates from a subprocess, and is likely not a problem with pip.
ERROR: Failed cleaning build dir for numpy
Failed to build numpy
ERROR: Could not build wheels for numpy, which is required to install pyproject.toml-based projects
我已经升级了 pip,但仍然出现此错误。因此,我认为 Python 版本要高得多才能与 numpy 版本兼容。
然后我尝试用Python 3.7.9创建另一个virtualenv,里面可以安装numpy==1.15.4,pandas==1.0.1。但是在py文件运行期间,我得到了这个错误:
Traceback (most recent call last):
File "c:/Users/myName/Documents/projectFolder/pm_artwork.py", line 6, in <module>
import pandas as pd
File "C:\Users\myName\Documents\projectFolder\pmArtwork37\lib\site-packages\pandas\__init__.py", line 17, in <module>
"Unable to import required dependencies:\n" + "\n".join(missing_dependencies)
ImportError: Unable to import required dependencies:
numpy:
Importing the multiarray numpy extension module failed. Most
likely you are trying to import a failed build of numpy.
If you're working with a numpy git repo, try `git clean -xdf` (removes all
files not under version control). Otherwise reinstall numpy.
Original error was: DLL load failed: The specified module could not be found.
我确实在某处读到我们需要在库中包含 DLL 文件,但我无法理解这意味着什么,你能详细说明一下这个问题吗?
这是我的 cx-Freeze 设置文件,这是我第一次使用 cx_Freeze,因此,包含和排除只是反复试验。对于包,我只需复制回 py 文件中导入的内容。也请就这些决定向我提出建议。
from __future__ import annotations
from cx_Freeze import Executable, setup
import os, sys
company_name = 'ABC'
product_name = 'Report_PDF_output'
version = '0.1'
# GUI applications require a different base on Windows
base = None
if sys.platform == 'win32':
base = 'Win32GUI'
executables = [Executable("pm_artwork.py",
base= base)
]
description = "Report as PDF for reference"
ROOT_DIR = os.path.realpath(os.path.join(os.path.dirname(__file__)))
# include_files = ['{}'.format(f) for f in os.listdir(ROOT_DIR)]
include_files = ['Fonts', 'Input (do not delete)', 'Settings (do not delete)']
print(include_files)
# excludes = []
excludes = ["tkinter",
"Tkinter",
"sqlite3",
"html",
"http" ,
"pyodbc_data",
"unittest",
"xml"]
packages = ['getpass', 'logging', 'time', 'os', 'shutil', 'sys', 'ast', 're', 'string',
'datetime', 'calendar', 'pandas', 'numpy', 'subprocess', 'traceback',
'smtplib', 'email', 'openpyxl', 'reportlab', 'arabic_reshaper', 'bidi.algorithm']
includes = ['numpy']
build_exe_options = {
'optimize': 2,
'packages':packages,
'includes': includes,
'include_files': include_files,
"excludes": excludes,
'include_msvcr':True,
'silent':True,
setup(
name = product_name,
version = version,
description = description,
executables = executables,
options={"build_exe": build_exe_options}
)
这是我的requirements.txt文件内容(不包括pandas和numpy版本,因为我上面提到过):
arabic-reshaper==3.0.0
cx-Freeze==6.15.5
cx-Logging==3.1.0
et-xmlfile==1.1.0
lief==0.13.2
numpy
openpyxl==3.1.2
pandas
Pillow==10.0.0
python-bidi==0.4.2
python-dateutil==2.8.2
pytz==2023.3
pywin32==306
reportlab==4.0.4
six==1.16.0
tzdata==2023.3
我需要知道,
冻结成exe文件后可以一起工作的Python版本、numpy版本和pandas版本是什么?
我们实际上在includes和packages中放入了什么,如何区分需求?
我们通常排除什么?特别是如果程序只需要执行以下操作:
此 exe 文件将触发 SAP GUI 从中获取数据,然后使用该数据创建 pdf 文件作为输出。
以下配置适用于我在 Windows 下的应用程序:
Python 3.10.11、numpy 1.25.2、pandas 2.1.0、cx_Freeze 6.15.7、从命令行执行的 setup.py 脚本 (Git Bash)
最新的 numpy 1.26.0 似乎不适用于 cx_Freeze 6.15.7
根据cx_Freeze文档:
includes:以逗号分隔的要包含的模块名称列表
packages:要包含的以逗号分隔的包列表,其中包括包中的所有子模块
我通常只放置模块或包,否则 cx_Freeze 无法正确包含这些模块或包,并导致冻结的应用程序出现错误。 cx_Freeze 代码具有针对大多数需要微调的常见包的钩子,并且这些钩子由 cx_Freeze 开发人员很好地维护,因此通常我不需要为我的应用程序显式放置任何模块或包,但这种情况时有发生.
作为一般策略,我从空的包含和包列表开始,然后添加任何必要的条目,直到冻结的应用程序正常工作。
我通常会排除相当大的Python标准库包,例如sqlite3、test、tkinter……只要应用程序不使用它们。
这里,作为一般策略,我从一个空的排除列表开始,并为最大的包添加条目,同时检查冻结的应用程序是否继续正常工作而没有错误。