1

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:

enter image description here

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
"""
Community
  • 1
  • 1
the_prole
  • 8,275
  • 16
  • 78
  • 163

2 Answers2

0

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"])]
    )
  • Why? The error says there is `no module named background_image`, and yet other modules are compiled fine. I imported `background_image.py` in `main.py` so shouldn't it just work? Anyways, I have not been able to make your solution work. – the_prole Nov 25 '14 at 17:19
  • @the_prole I realized at least part of the problem with why you couldn't get it to work (fixed above). Also, you are not importing `background_image.py`, so you need to add it as a data file. You may need to take a look at documentation to get it to work. –  Nov 25 '14 at 17:24
0

Don't use bundle_files = 1, it has too many problems. Suggest to use bundle_files = 2 and then use e.g. 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.

Werner
  • 2,086
  • 1
  • 15
  • 14
  • The idea was that I wouldn't have to use an installer. It's why I didn't use Inno. I will take your other suggestions into consideration, thanks. – the_prole Nov 25 '14 at 17:32