python - pyinstaller 不导入子模块

标签 python pyinstaller

我似乎无法让 pyinstaller 正确打包模块。

模块结构示例

myapp/
      __main__.py
      mysubmodule.py

示例 __main__.py内容

"""My __main__.py."""

import myapp.mysubmodule

print("Done")

如果我运行 python -m myapp它运行时没有错误并打印 Done

如果我运行 pyinstaller myapp我收到错误消息,指出它是一个目录。

如果我运行 pyinstaller myapp/__main__.py它构建但是当我执行 dist/__main__/__main__ 时生成的我得到 ModuleNotFoundError: No module named 'myapp' .

如何让 pyinstaller 包含 myapp作为模块以便可以导入?

最佳答案

添加__init__.py,否则myapp它不是有效的模块/包。

缺少的文件是:

  • myapp/__init__.py

编辑:

您可以将__init__.py留空。

阅读文档 here .

The __init__.py files are required to make Python treat the directories as containing packages; this is done to prevent directories with a common name, such as string, from unintentionally hiding valid modules that occur later on the module search path. In the simplest case, __init__.py can just be an empty file, but it can also execute initialization code for the package or set the __all__ variable, described later.

关于python - pyinstaller 不导入子模块,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44287817/

相关文章:

python - 程序忽略 if 语句,并在 False 时打印 True

python - Django - AttributeError : 'NoneType' object has no attribute 'first_name'

python - 用python2.7阅读docx

python - 使用 PyInstaller 自定义日志处理程序和格式化程序

python - 关于 Python 可执行文件中缺少 GTK 图标的警告

python - 如何在我的类中正确集成 add 和 mul 方法?

python - pyodbc 始终连接到 master 数据库

python - 使用 Windows 机器为 Linux 构建 Python 独立可执行文件

python - 应用程序包中的 PyInstaller Tkinter 窗口分辨率低,但应用程序中没有

如果没有控制台构建,pyinstaller 程序将无法运行