【问题标题】:Relative import error with py2exepy2exe 的相对导入错误
【发布时间】:2016-04-10 12:38:37
【问题描述】:

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

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

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

【问题讨论】:

  • 需要更多信息,例如,您的项目文件结构。但是尝试将 py2exe 路径添加到您的 PYTHONPATH?

标签: python py2exe relative-import


【解决方案1】:

在你的 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 将子包称为:

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

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

from ..utilities.functions import *

向 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
               }
       }
    )

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-07-13
    • 2020-02-15
    • 2016-06-13
    • 1970-01-01
    • 2021-11-04
    • 1970-01-01
    • 2016-03-07
    相关资源
    最近更新 更多