假设这是我的文件结构:
main.py
modules
|--> feature1.py
|--> feature2.py
|--> feature3.py
我的main.py
代码如下:
from modules.feature1 import Awesomefeature
...
我为PyInstaller使用了以下规范文件:
# -*- mode: python -*-
block_cipher = None
a = Analysis(['main.py'],
runtime_hooks=[],
excludes=[],
win_no_prefer_redirects=False,
win_private_assemblies=False,
cipher=block_cipher)
pyz = PYZ(a.pure, a.zipped_data,
cipher=block_cipher)
exe = EXE(pyz,
a.scripts,
a.binaries,
a.zipfiles,
a.datas,
name='main',
debug=False,
strip=False,
upx=False,
runtime_tmpdir=None,
console=True , icon='icon.ico')
不幸的是,在将代码编译为Windows可执行文件并执行此(main.exe文件)后,我收到以下消息:
ModuleNotFoundError: no module named 'modules'
使用pyinstaller时,是否完全不可能在其中包含pythoncode子文件夹?
尝试使用指向根项目目录的路径将pathex列表添加到spec文件中。
a = Analysis(['main.py']),
pathex=['C:/Users/<user>/Path to the root directory'],
# rest of spec file
有时您需要将模块位置添加到pathex列表中。这是pyinstaller将首先搜索的路径列表。