Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

python 3.x - cx_freeze creating exe but encountering: zipimport.ZipImportError: can't find module 'mainprogram__main__' when running it

I have a repository structure as such:

MinimalExample
├── _buildscripts
│   ├── setup.py
│
├── _MainProgram
│   ├──_modules1
│   │  ├──timescalculation.py
│   │
│   ├──_modules2
│   │  ├──divisioncalculation.py
│   │
│   │──mainprogram.py
│
│──README.md

My setup.py is:

import sys
import setuptools
import os
from pathlib import Path
from cx_Freeze import setup, Executable

# adding this path to sys so modules are searched from it
sys.path.append(os.path.abspath("../MainProgram")) # maybe this causes the problem?

# just to see what is in sys
for dire in sys.path:
    print(dire)

# PATH DEFINITIONS
mainprogram_path = Path(os.path.abspath('MainProgram/mainprogram.py')) # OR maybe this causes the problem?

# OTHER BUILD_EXE OPTIONS DEFINITIONS
includes = ["os"]
packages = ["modules1", "modules2"]
replace_paths = [("*", "")]
base = None

# EXECUTABLE DEFINITION
executables = [Executable(
                            script=mainprogram_path, 
                            shortcut_name = "Example", 
                            shortcut_dir="DesktopFolder", 
                            base=base
                            )
                ]

# SETUP DEFINITION
setup(  name = "ExampleProgram",
        author = "Me",
        version = "1.0",
        options = {"build_exe": {
                                "packages": packages,
                                "includes": includes,
                                "optimize": 1,
                                "replace_paths": replace_paths},
                    "bdist_msi": {
                        "upgrade_code": "{9446b224-e087-4502-a791-5bd132065ccd}"} # for example use only
                    },
        executables = executables)

And here is mainprogram.py

from modules1 import timescalculation
from modules2 import divisioncalculation
import time

def main():
    num1 = float(input("Give first num: "))
    num2 = float(input("Give second num: "))
    times = timescalculation.timescalculation(num1, num2)
    print(num1, " times ", num2, "is", times)
    division = divisioncalculation.divisioncalculation(num1, num2)
    print(num1, " devided by ", num2, "is", division)
    time.sleep(5)
    
if __name__ == "__main__":
    main()

modules1 -> timescalculation.py

def timescalculation(num1, num2):
    return num1 * num2

modules2 -> divisioncalculation.py

def divisioncalculation(num1, num2):
    return num1 / num2

The code is just for repeating the problem I am having. But the setup.py is pretty much what I am using.

After I run python setup.py bdist_msi the script builds successfully but after running the exe file from console I get:

Traceback (most recent call last):
  File "__startup__.py", line 74, in run
  File "Console.py", line 33, in run
  File "<frozen zipimport>", line 159, in get_code
  File "<frozen zipimport>", line 721, in _get_module_code
zipimport.ZipImportError: can't find module 'mainprogram__main__'

Question:

Is this because I am running setup.py from a different folder? Is it possible to run it from where it is now?

If it is possible to run setup.py from a different folder than mainprogram.py, how would I do it?

But if I run setup from the same folder the mainprogram.py resides I don't have this problem, but I'd like to have it in it's own folder with icon.ico and other stuff. This is my first time using cx_freeze and maybe it is just against convention to have setup.py in different folder?

question from:https://stackoverflow.com/questions/65840173/cx-freeze-creating-exe-but-encountering-zipimport-zipimporterror-cant-find-mo

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

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...