Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
109 views
in Technique[技术] by (71.8m points)

python - 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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...