如何解决找不到 Pyinstaller 模块错误

How to resolve Pyinstaller module not found error

我正在尝试使用 Pyinstaller 将我的项目打包成可执行文件。目前,我正在基于 Ubuntu 的 PC 上进行概念验证,之后我计划切换到 Windows 来构建一个 .exe,它可以是 运行 on windows.问题是,在我构建可执行文件并 运行 之后,它找不到在启动文件中导入的本地包之一。这是我的项目结构:

.
├── hook-streamlit.py
├── __init__.py
├── models
│   ├── __init__.py
│   ├── loader.py
│   ├── nn.py
│   └── runner.py
├── notebooks
├── README.md
├── requirements.txt
├── stapp
│   ├── __init__.py
│   ├── main.py
│   └── session_state.py
├── startup.py
├── startup.spec
├── test_data
│   ├── test_x.csv
│   ├── test_y.csv
│   ├── train_x.csv
│   └── train_y.csv
├── tkapp.py
├── unipredictor-icon.ico
├── UniPredictor.spec
└── utils.py

startup.py是启动脚本,内容如下:

import os
import subprocess
import shlex

from models import nn, loader, runner
from stapp import main

subprocess.call(shlex.split(f"streamlit run {os.path.join('stapp', 'main.py')} --global.developmentMode=false"))

即使我不需要启动脚本中的 modelsstapp 包,我还是导入了它们,只是为了确保 pyinstaller 解决依赖关系,因为我使用子进程到 运行 应用程序。但即便如此,在构建和 运行 可执行文件后,我仍然得到 ModuleNotFoundError: No module named 'models'。此错误来自 stapp.main,其中导入了 models。我尝试将 modelsstapp 添加到 hiddenimports 但没有成功。我会认为,既然项目根目录被添加到 PYTHONPATH,它不应该与本地包有任何问题。即使出于某种原因确实如此, import 和 hiddenimports 也应该处理这个问题。这是我当前的规格文件:

# -*- mode: python ; coding: utf-8 -*-

block_cipher = None


a = Analysis(['startup.py'],
             pathex=['/home/kenneth/PycharmProjects/universal_predictor'],
             binaries=[],
             datas=[],
             hiddenimports=['models', 'stapp'],
             hookspath=['.'],
             runtime_hooks=[],
             excludes=['torch.distributions'],
             win_no_prefer_redirects=False,
             win_private_assemblies=False,
             cipher=block_cipher,
             noarchive=False)
pyz = PYZ(a.pure, a.zipped_data,
             cipher=block_cipher)
exe = EXE(pyz,
          a.scripts,
          a.binaries,
          a.zipfiles,
          a.datas,
          [],
          name='startup',
          debug=False,
          bootloader_ignore_signals=False,
          strip=False,
          upx=True,
          upx_exclude=[],
          runtime_tmpdir=None,
          console=False , icon='unipredictor-icon.ico')

对于如何使 pyinstaller 包含项目的本地包的任何帮助,我将不胜感激。谢谢!

问题正是显示的内容。您需要为模型扩展 PYTHONPATH。在您的规范文件中也添加模型的包路径。

# -*- mode: python ; coding: utf-8 -*-

block_cipher = None


a = Analysis(['startup.py'],
             pathex=['/home/kenneth/PycharmProjects/universal_predictor', 
'/home/kenneth/PycharmProjects/universal_predictor/models'],
             binaries=[],
             datas=[],
             hiddenimports=['models', 'stapp'],
             hookspath=['.'],
             runtime_hooks=[],
             excludes=['torch.distributions'],
             win_no_prefer_redirects=False,
             win_private_assemblies=False,
             cipher=block_cipher,
             noarchive=False)
pyz = PYZ(a.pure, a.zipped_data,
             cipher=block_cipher)
exe = EXE(pyz,
          a.scripts,
          a.binaries,
          a.zipfiles,
          a.datas,
          [],
          name='startup',
          debug=False,
          bootloader_ignore_signals=False,
          strip=False,
          upx=True,
          upx_exclude=[],
          runtime_tmpdir=None,
          console=False , icon='unipredictor-icon.ico')

我通过加载所需的包和模块作为数据解决了这个问题。

# -*- mode: python ; coding: utf-8 -*-

block_cipher = None


a = Analysis(['startup.py'],
             pathex=['/home/kenneth/PycharmProjects/universal_predictor'],
             binaries=[],
              datas=[('.streamlit', '.streamlit'), ('.data', '.data'), ('models', 'models'), ('stapp', 'stapp'), ('utils.py', '.')],
             hiddenimports=[],
             hookspath=['.'],
             runtime_hooks=[],
             excludes=['torch.distributions'],
             win_no_prefer_redirects=False,
             win_private_assemblies=False,
             cipher=block_cipher,
             noarchive=False)
pyz = PYZ(a.pure, a.zipped_data,
             cipher=block_cipher)
exe = EXE(pyz,
          a.scripts,
          a.binaries,
          a.zipfiles,
          a.datas,
          [],
          name='startup',
          debug=False,
          bootloader_ignore_signals=False,
          strip=False,
          upx=True,
          upx_exclude=[],
          runtime_tmpdir=None,
          console=False , icon='unipredictor-icon.ico')