Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

enter image description here

like image 687
multigoodverse Avatar asked Jan 06 '16 12:01

multigoodverse


1 Answers

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

like image 170
mabe02 Avatar answered Oct 10 '22 06:10

mabe02