gpt4 book ai didi

python - py2exe 的相对导入错误

转载 作者:太空狗 更新时间:2023-10-29 17:20:19 24 4
gpt4 key购买 nike

我试图为一个简单的 Python 脚本生成一个可执行文件。我的 setup.py 代码如下所示:

from distutils.core import setup
import py2exe
setup(console=["script.py"])

但是,我收到屏幕截图中显示的错误。有什么我可以尝试解决这个问题的吗?我正在使用 Windows 10。

enter image description here

最佳答案

在你的mf3.py中你似乎是importing beyond the top level .

假设您的项目结构如下:

folder/
main.py
mod/
__init__.py
components/
__init__.py
expander.py
language_id.py
utilities/
__init__.py
functions.py

首先要确保

main.py refers to the subpackages as:

from mod.components.expander import *
from mod.utilities.functions import *

expander.py 和 language_id.py 可以通过以下方式访问 functions.py:

从 ..utilities.functions 导入 *

将选项添加到您的 setup.py

您还可以使用更多 py2exe options为了导入项目所需的所有模块和包。例如

# setup.py
from distutils.core import setup
import py2exe
setup(console=["script.py"],
options={
"py2exe":{
"optimize": 2,
"includes": ["mf1.py", "mf2.py", "mf3.py"], # List of all the modules you want to import
"packages": ["package1"] # List of the package you want to make sure that will be imported
}
}
)

通过这种方式,您可以强制导入项目中缺少的脚本

关于python - py2exe 的相对导入错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34632845/

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