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