PyInstaller ModuleNotFoundError --paths 标志似乎不起作用

PyInstaller ModuleNotFoundError --paths flag seems to not work

我使用 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 设置为包含 ai4eutilscameratraps 以及 运行 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

我知道有很多关于这种类型或错误的话题。我尝试了很多可能的解决方案,但 none 似乎有效。我尝试了以下方法:

仅供参考,这就是我创建环境的方式 运行 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

PYTHONPATH 几乎总是局部最小值。根据我的经验,它只会使长 运行 中的事情复杂化。我建议第 1 步是从您的工作流程中删除 PYTHONPATH 并了解 python packagens 和可编辑的 intsall。从长远来看,这将使开发变得更加容易 运行.

PYTHONPATH 基本上是作为一种让“脚本”访问其他模块而无需实际安装包的方式开始的。这在 virtualenv 和 conda 之前的糟糕日子里更有意义,但现在使用包结构更容易、更有条理。

尝试像典型的可安装 python 库一样构建您的项目。例如

.
├── .git
├── ImSep_files
│  ├── ai4eutils
│  ├── cameratraps
│  │  └── detection
│  │     └── run_tf_detector.py
│  └── ImSep
│     └── ImSep_GUI.py
└── setup.py

确保您可以 pip install . 从您的根目录。您应该有一些从中导入的顶级包名称(在这种情况下,我任意选择 ImgSep_Files 作为您的库名称,但它可以是任何名称)。那么你应该能够始终使用绝对或相对包语法导入,比如

from .detection.run_tf_detector import ImagePathUtils, TFDetector

最终的考验是你是否可以 运行 python -m ImSep_files.cameratraps.detection.run_tf_detector没有 使用PYTHONPATH。这意味着您的导入结构正确,pyinstaller 应该可以毫无问题地获取您的依赖项。

更新:这里有一个带有 setup.py 的简单包示例。我选择了 setup.py,尽管那有点老派,而且事情正在朝着 pyproject.toml 的方向发展,因为有更多关于这种风格的文档:

from setuptools import setup, find_packages

setup(
    name="my_package",
    description="An example setup.py",
    license="MIT",
    packages=find_packages(),
    python_requires=">=3.7",
    zip_safe=False,
    install_requires=[
        "tensorflow",
    ],
    classifiers=[
        "Programming Language :: Python :: 3.7",
    ],
    entry_points={
        "console_scripts": [
            "run_tf_detector=my_package.scripts.run_tf_detector:main",
            "imsep_gui=my_package.gui.gui:main",
        ]
    },
)

然后我有这样的布局:

.
└── my_project_name
   ├── .git
   ├── my_package
   │  ├── gui
   │  │  ├── gui.py
   │  │  └── gui_utils.py
   │  ├── scripts
   │  │  └── run_tf_detector.py
   │  └── detection
   │     └── tf_detector.py
   ├── README.md
   ├── setup.py
   └── tests
      └── test_tf_detector.py

my_project_name 是我的“回购根”。 my_package 是我的包裹的名字。我会导入 from my_package.detection.tf_detector import TFDetector。在这种情况下,我会将所有 类 和逻辑放在 tf_detector.py 中,然后 run_tf_detector.py 基本上就是:

import sys
from my_package.detection.tf_detector import TFDetector


def main(args=None):
    args = args or sys.argv
    detector = TFDetector()
    detector.detect(args)

if __name__ == __main__:
    main()

GUI 遵循一个简单的模式,gui.py 包含启动 gui 的入口点。这种组织使您的功能代码与作为脚本的 运行ning 的具体细节分开。例如,它使检测器变得容易 运行 作为 CLI 脚本,或作为 GUI 的一部分,或作为您可以导入的库。

Entry points are used to tell the installer "this is a thing that you run or a plugin". Some more 信息。