【发布时间】:2021-12-09 20:58:15
【问题描述】:
我使用 Tkinter 构建了一个简单的 GUI,我想将其冻结为独立的可执行文件。我在 conda 环境中执行此操作。使用 OSX 10.15.7、python 3.7、PyInstaller 4.5.1 和 conda 4.10.0。文件夹结构如下(简化):
- ImSep_files
| - ai4eutils
| - ImSep
| | - ImSep_GUI.py
| - cameratraps
| - detection
| - run_tf_detector.py
脚本调用 ai4eutils 和 cameratraps 文件夹中的其他脚本。如果我创建一个 conda 环境,设置PYTHONPATH 以包含ai4eutils 和cameratraps 的路径,然后运行python ImSep_GUI.py,没有问题。 GUI 打开并完美运行。但是,如果我执行完全相同的操作但运行 pyinstaller 而不是 python,它会创建一个打开 GUI 但在按下按钮时抛出错误的 exe。
File "/Users/peter/Applications/ImSep_files/cameratraps/detection/run_tf_detector_batch.py", line 56, in <module>
from detection.run_tf_detector import ImagePathUtils, TFDetector
ModuleNotFoundError: No module named 'detection.run_tf_detector'
这意味着pyinstaller 找不到run_tf_detector.py 文件。我尝试添加 --paths 标志,例如:
pyinstaller --onefile --windowed --name='ImSep' --icon='imgs/logo_small_bg.icns' --paths=/Users/peter/Applications/ImSep_files --paths=/Users/peter/Applications/ImSep_files/ai4eutils --paths=/Users/peter/Applications/ImSep_files/cameratraps --paths=/Users/peter/Applications/ImSep_files/cameratraps/detection ImSep_GUI.py
我知道有很多关于这种类型或错误的主题。我尝试了许多潜在的解决方案,但似乎都没有奏效。我尝试了以下方法:
- 使用
--hidden-import标志,正如 HHest 建议的 here。如果尝试不同的版本:--hidden-import detection.run_tf_detector、--hidden-import cameratraps.detection.run_tf_detector、--hidden-import cameratraps.detection等。 - 根据 user1251007 的建议 here,使用上述路径调整
hiddenimports=[],行。 - 将
sys.path.append(path/to/run_tf_detector.py)添加到ImSep_GUI.py的顶部。 - 按照 Fivef 的建议 here 将
pyinstaller降级到 3.1。 - 在
hooks文件夹中创建带有detection.run_tf_detector的hook.py并将其添加为--additional-hooks-dir=hooks,正如 Legorooj 建议的 here。 - 按照 Ken4scholars here 的建议,将所需模块作为数据加载到规范文件中。
- 将
run_tf_detector.py复制到与ImSep.exe同级的文件夹中,按照Wayne Zhang的建议here。 - 从父目录调用
pyinstaller,按照here的建议,全部或无。 - 按照 Habeeb Rahman K T 的建议 here,将
pyinstaller安装在存在ImSep_GUI.py的同一目录中。 - 安装
pyinstaller使用conda-forge而不是pip,正如建议的here 通过管道管道。
仅供参考,这就是我创建环境并运行pyinstaller的方式:
conda create --name imsepcondaenv python=3.7 -y
conda activate imsepcondaenv
pip install tensorflow==1.14 pillow==8.4.0 humanfriendly==10.0 matplotlib==3.4.3 tqdm==4.62.3 jsonpickle==2.0.0 statistics==1.0.3.5 requests==2.26.0
conda install -c conda-forge pyinstaller -y
cd ~/Applications/ImSep_files
export PYTHONPATH="$PYTHONPATH:$PWD/ai4eutils:$PWD/cameratraps"
cd ImSep
pyinstaller --onefile --windowed --name='ImSep' --icon='imgs/logo_small_bg.icns' --paths=/Users/peter/Applications/ImSep_files --paths=/Users/peter/Applications/ImSep_files/ai4eutils --paths=/Users/peter/Applications/ImSep_files/cameratraps --paths=/Users/peter/Applications/ImSep_files/cameratraps/detection ImSep_GUI.py
有人知道我做错了什么吗?
PS:对于 OSX 和 UNIX 用户,可以获得可重现的示例:
mkdir ImSep_files
cd ImSep_files
git clone https://github.com/Microsoft/cameratraps -b tf1-compat
git clone https://github.com/Microsoft/ai4eutils
git clone https://github.com/PetervanLunteren/ImSep.git
curl --output md_v4.1.0.pb https://lilablobssc.blob.core.windows.net/models/camera_traps/megadetector/md_v4.1.0/md_v4.1.0.pb
【问题讨论】:
标签: python-3.x pyinstaller modulenotfounderror