简体   繁体   中英

Relative import error with py2exe

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. Eg

# 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM