py2exe 的相对导入错误 [英] Relative import error with py2exe
问题描述
我试图为一个简单的 Python 脚本生成一个可执行文件.我的 setup.py 代码如下所示:
from distutils.core 导入设置导入py2exe设置(控制台 = [script.py"])
但是,我收到了屏幕截图中显示的错误.有什么我可以尝试解决这个问题的吗?我使用的是 Windows 10.
似乎在你的 mf3.py 中你是 超出顶层的导入.
假设您的项目结构如下:
文件夹/主文件模/__init__.py组件/__init__.py扩展器.py语言id.py公用事业/__init__.py函数.py
首先确保
<块引用><块引用>main.py 引用子包为:
from mod.components.expander import *从 mod.utilities.functions 导入 *
expander.py 和 language_id.py 可以通过以下方式访问 functions.py:
from ..utilities.functions import *
向 setup.py 添加选项
您还可以使用更多 py2exe 选项,以便您导入所有项目所需的模块和包.例如
# setup.py从 distutils.core 导入设置导入py2exe设置(控制台 = [script.py"],选项={py2exe":{优化":2,"includes": ["mf1.py", "mf2.py", "mf3.py"], # 要导入的所有模块的列表"packages": ["package1"] # 要确保将导入的包列表}})
这样你就可以强制导入你的项目缺少的脚本
I was trying to generate an executable for a simple Python script. My setup.py code looks like this:
from distutils.core import setup
import py2exe
setup(console=["script.py"])
However, I am getting the error shown in the screenshot. Is there something I could try to fix this? I am using Windows 10.
It seems that in your mf3.py you are importing beyond the top level.
Let's suppose that your project structure is as follows:
folder/
main.py
mod/
__init__.py
components/
__init__.py
expander.py
language_id.py
utilities/
__init__.py
functions.py
First make sure that
main.py refers to the subpackages as:
from mod.components.expander import * from mod.utilities.functions import *
expander.py and language_id.py have access to functions.py with:
from ..utilities.functions import *
Add options to your setup.py
You can also use more py2exe options in order that you are importing all the modules and the packages required by your project. E.g.
# 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
}
}
)
In this way you can force the import of the missing script of your project
这篇关于py2exe 的相对导入错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!