gpt4 book ai didi

python - pyinstaller 不导入子模块

转载 作者:太空宇宙 更新时间:2023-11-03 15:10:07 24 4
gpt4 key购买 nike

我似乎无法让 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com