我有别人的Python代码,在将数据库调用更改为API调用之前,我正在将其从3.7升级到3.12。 Python 代码使用 PyInstaller 编译成可执行文件,以便根据需要分发。
如果我运行 PyInstaller 命令,它会编译良好,创建一个可执行文件和一个包含所有所需文件的“_internal”文件夹(尽管以前的版本在与可执行文件相同的位置存在更多文件。当我双击可执行文件时,我收到运行时错误,如下所示:
Traceback (most recent call last):
File "D:\SourceCode\ODL\ODL_App.py", line 10, in <module>
import core.core_utils
File "<frozen importlib._bootstrap>", line 1360, in _find_and_load
File "<frozen importlib._bootstrap>", line 1331, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 935, in _load_unlocked
File "PyInstaller\loader\pyimod02_importers.py", line 419, in exec_module
File "core\__init__.py", line 5, in <module>
File "<frozen importlib._bootstrap>", line 1360, in _find_and_load
File "<frozen importlib._bootstrap>", line 1331, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 935, in _load_unlocked
File "PyInstaller\loader\pyimod02_importers.py", line 419, in exec_module
File "core\ODL_Method.py", line 4, in <module>
File "<frozen importlib._bootstrap>", line 1360, in _find_and_load
File "<frozen importlib._bootstrap>", line 1331, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 935, in _load_unlocked
File "PyInstaller\loader\pyimod02_importers.py", line 419, in exec_module
File "als_microservices\__init__.py", line 2, in <module>
File "<frozen importlib._bootstrap>", line 1360, in _find_and_load
File "<frozen importlib._bootstrap>", line 1331, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 935, in _load_unlocked
File "PyInstaller\loader\pyimod02_importers.py", line 419, in exec_module
File "als_microservices\aqc_stl.py", line 11, in <module>
File "<frozen importlib._bootstrap>", line 1360, in _find_and_load
File "<frozen importlib._bootstrap>", line 1331, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 935, in _load_unlocked
File "PyInstaller\loader\pyimod02_importers.py", line 419, in exec_module
File "scipy\stats\__init__.py", line 610, in <module>
File "<frozen importlib._bootstrap>", line 1360, in _find_and_load
File "<frozen importlib._bootstrap>", line 1331, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 935, in _load_unlocked
File "PyInstaller\loader\pyimod02_importers.py", line 419, in exec_module
File "scipy\stats\_stats_py.py", line 37, in <module>
File "<frozen importlib._bootstrap>", line 1360, in _find_and_load
File "<frozen importlib._bootstrap>", line 1331, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 935, in _load_unlocked
File "PyInstaller\loader\pyimod02_importers.py", line 419, in exec_module
File "scipy\sparse\__init__.py", line 293, in <module>
File "<frozen importlib._bootstrap>", line 1360, in _find_and_load
File "<frozen importlib._bootstrap>", line 1331, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 935, in _load_unlocked
File "PyInstaller\loader\pyimod02_importers.py", line 419, in exec_module
File "scipy\sparse\_base.py", line 5, in <module>
File "<frozen importlib._bootstrap>", line 1360, in _find_and_load
File "<frozen importlib._bootstrap>", line 1331, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 935, in _load_unlocked
File "PyInstaller\loader\pyimod02_importers.py", line 419, in exec_module
File "scipy\sparse\_sputils.py", line 10, in <module>
File "<frozen importlib._bootstrap>", line 1360, in _find_and_load
File "<frozen importlib._bootstrap>", line 1331, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 935, in _load_unlocked
File "PyInstaller\loader\pyimod02_importers.py", line 419, in exec_module
File "scipy\_lib\_util.py", line 18, in <module>
File "<frozen importlib._bootstrap>", line 1360, in _find_and_load
File "<frozen importlib._bootstrap>", line 1331, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 935, in _load_unlocked
File "PyInstaller\loader\pyimod02_importers.py", line 419, in exec_module
File "scipy\_lib\_array_api.py", line 21, in <module>
File "<frozen importlib._bootstrap>", line 1360, in _find_and_load
File "<frozen importlib._bootstrap>", line 1331, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 935, in _load_unlocked
File "PyInstaller\loader\pyimod02_importers.py", line 419, in exec_module
File "scipy\_lib\array_api_compat\numpy\__init__.py", line 1, in <module>
File "<frozen importlib._bootstrap>", line 1360, in _find_and_load
File "<frozen importlib._bootstrap>", line 1331, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 935, in _load_unlocked
File "PyInstaller\loader\pyimod02_importers.py", line 419, in exec_module
File "numpy\f2py\__init__.py", line 19, in <module>
File "<frozen importlib._bootstrap>", line 1360, in _find_and_load
File "<frozen importlib._bootstrap>", line 1331, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 935, in _load_unlocked
File "PyInstaller\loader\pyimod02_importers.py", line 419, in exec_module
File "numpy\f2py\f2py2e.py", line 23, in <module>
File "<frozen importlib._bootstrap>", line 1360, in _find_and_load
File "<frozen importlib._bootstrap>", line 1331, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 935, in _load_unlocked
File "PyInstaller\loader\pyimod02_importers.py", line 419, in exec_module
File "numpy\f2py\crackfortran.py", line 159, in <module>
File "<frozen importlib._bootstrap>", line 1360, in _find_and_load
File "<frozen importlib._bootstrap>", line 1331, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 935, in _load_unlocked
File "PyInstaller\loader\pyimod02_importers.py", line 419, in exec_module
File "numpy\f2py\auxfuncs.py", line 19, in <module>
File "<frozen importlib._bootstrap>", line 1360, in _find_and_load
File "<frozen importlib._bootstrap>", line 1331, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 935, in _load_unlocked
File "PyInstaller\loader\pyimod02_importers.py", line 419, in exec_module
File "numpy\f2py\cfuncs.py", line 19, in <module>
AttributeError: 'NoneType' object has no attribute 'write'
显然我可以看到最后一行包含错误的症结所在,但我不知道该模块或该模块中的哪一行适用,因为它引用了许多模块。
现在,当通过 PyCharm 运行时,我可以让应用程序运行,尽管它在数据库函数处超时,因此我无法通过单步执行代码来查看错误发生在哪里。
如果我在 PyCharm 终端或启动 PyInstaller 的窗口中输入命令“python --version”,我会得到相同的 Python 版本。如果我导入 sys,然后在 PyCharm Python 窗口中执行 print(sys.version),我也会得到相同的版本。
现在,当一切都在 Python 3.7 上运行时,这个用法就可以工作了,所以我知道这可能是因为 Python 和模块之间的版本不匹配,或者是某些已被弃用的东西或现在更改了的函数。
最大的问题是:如何缩小范围以解决问题。
这是 numpy 库中的一个错误,并已被其作者承认。详细信息请查看链接:https://github.com/numpy/numpy/pull/27021/commits/e10db14228067552b900b3399ea9bb1b552dd2d0
我的问题是更改虚拟环境中的库不起作用,因为它正在使用我的用户帐户下保存的库,我猜这些是全局库。要查看 PyInstaller 实例正在使用哪些库,请密切注意它何时构建 EXE,以查看它包含运行时挂钩的位置。