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

wtlkbnrh  于 11个月前  发布在  Python
关注(0)|答案(3)|浏览(195)

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

.
├── 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被导入。我尝试添加这两个modelsstapphiddenimports没有成功。我认为既然项目根目录被添加到PYTHONPATH,它应该不会有任何本地包的问题。即使出于某种原因,导入和hiddenimports应该处理这个问题。下面是我当前的spec文件:

# -*- 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包括项目的本地软件包的帮助。谢谢!

qvtsj1bj

qvtsj1bj1#

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

# -*- 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')

字符串

lnxxn5zx

lnxxn5zx2#

这个问题正是所显示的。你需要为模型扩展PYTHONPATH。在你的spec文件中也为模型添加包路径。

# -*- 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')

字符串

fdx2calv

fdx2calv3#

我曾经遇到过类似的情况,我的解决方案是从根级别删除init.py。
此外,作为一个有用的通知,我想看到我自己之前,我深入到故障排除,不要忘记检查,如果你已经有所有需要的软件包在venv。

相关问题