简体   繁体   中英

Cannot compile two .py files into stand alone executable using py2exe

I cannot compile two .py files into stand alone executable that does not need to be installed using py2exe . I followed the instructions on this post post and wrote my setup file as follows:

from distutils.core import setup
import py2exe
import sys, os

setup(
    options = {'py2exe': {'bundle_files': 1, 'compressed': True}},
    windows=[{'script': "main.py"}],
    zipfile = None,
    )

My problem however, is that I have two .py files , my main file main.py , and my background_image.py file (which contains base 64 image strings). As a result, py2exe compiles these two file separately as you can see here in this image:

在此处输入图片说明

As a result, I receive the following error when I try to run the main compiled file.

Traceback (most recent call last):
  File 'main.py", line 8, in <module>
ImportError: No Module named 'background_image'

This is a reduced version of my program from main.py ; the program draws a canvas with a background.

import tkinter as tk     
from PIL import ImageTk, Image
import background_images

#image variables
background = images.background_image
close_icon = images.close_icon

#root window creation
root=tk.Tk()
root.geometry(600, 600)

#canvas widget
photo = tk.PhotoImage(data=background)
width, height = photo.width(), photo.height()
canvas = tk.Canvas(root, width=width, height=height, bd=-2)
canvas.pack()
canvas.create_image(0, 0, image=photo, anchor="nw")

root.mainloop()

and here is shortened background_image from background_images.py

background_image = """
iVBORw0KGgoAAAANSUhEUgAA #... continues on
"""

Add the background image as a data file:

setup(
    options = {'py2exe': {'bundle_files': 1, 'compressed': True}},
    windows=[{'script': "main.py"}],
    zipfile = None,
    data_files= [ ("prog",["background_image.py"])]
    )

Don't use bundle_files = 1, it has too many problems. Suggest to use bundle_files = 2 and then use eg InnoSetup to create a one file installer. If that doesn't solve it please provide a small self contained sample of your main.py, plus bg_image.py plus setup.py.

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