[英]try/except works in pycharm but doesn't work in exe after compiling using pyinstaller
Test code:测试代码:
## file name: test.py
import scanpy as sc
print(sc.__version__)
Then install scanpy
using pip然后使用 pip 安装
scanpy
pip install scanpy==1.8.2
pip install pyinstaller==4.7
I generated the exe file using the command line below:我使用下面的命令行生成了 exe 文件:
python -m PyInstaller --noupx test.py
I then tried to run test.exe
.然后我尝试运行
test.exe
。 I got the error message:我收到错误消息:
Traceback (most recent call last):
File "scanpy\_metadata.py", line 28, in <module>
File "pathlib.py", line 1221, in read_text
File "pathlib.py", line 1208, in open
File "pathlib.py", line 1063, in _opener
FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\xie186\\PycharmProjects\\pitaya\\test_exe\\dist\\test\\pyproject.toml'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "test.py", line 1, in <module>
File "PyInstaller\loader\pyimod03_importers.py", line 476, in exec_module
File "scanpy\__init__.py", line 3, in <module>
File "PyInstaller\loader\pyimod03_importers.py", line 476, in exec_module
File "scanpy\_metadata.py", line 38, in <module>
File "scanpy\_compat.py", line 25, in pkg_metadata
File "importlib_metadata\__init__.py", line 984, in metadata
File "importlib_metadata\__init__.py", line 561, in from_name
importlib_metadata.PackageNotFoundError: No package metadata was found for scanpy
[20028] Failed to execute script 'test' due to unhandled exception!
But when I run this script in pycharm, it works fine.但是当我在 pycharm 中运行这个脚本时,它工作正常。 I checked the code related to this error: https://github.com/theislab/scanpy/blob/16e62d1336a95966238113cdb5ab50f7d9f880ce/scanpy/_metadata.py#L24
我检查了与此错误相关的代码: https://github.com/theislab/scanpy/blob/16e62d1336a95966238113cdb5ab50f7d9f880ce/scanpy/_metadata.py#L24
In pycharm, the try
part will fail and the code in except
part will be executed.在 pycharm 中,
try
部分会失败,而except
部分的代码会被执行。 But in the exe
file, it seems that the app will exit after the try
part fails.但是在
exe
文件中,好像try
部分失败后app会退出。
Any help will be appreciated!任何帮助将不胜感激!
Adding --recursive-copy-metadata scanpy
solved my problem:添加
--recursive-copy-metadata scanpy
解决了我的问题:
python -m PyInstaller --recursive-copy-metadata scanpy --noupx ..\test.py
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.