I'm having an issue with running a script as an executable. I believe it has to do with MEIXXXXX but am not sure how to remedy it. The script below runs perfectly when run in python 3.10.5 but when I create an executable the file won't save or create.
import os
from tkinter import Button, Entry, Label, Tk
import barcode
from barcode.writer import ImageWriter
root = Tk()
root.minsize(400, 200)
# Add Title from Filename
title = os.path.basename(__file__)[0:-3]
root.title(title.title())
# Get the path of the running file
fp = os.path.dirname(__file__)
os.chdir(fp)
def calculated(event=None):
# Define content of the barcode as a string
# Get entry as an input for the barcode 11 char or 12 if you know the checksum digit.
number = e1.get()
# calulate the checksum and store the full barcode in the variable 'cs'
cs = barcode.UPCA(number).get_fullcode()
# print(cs)
# Get the required barcode format
barcode_format = barcode.get_barcode_class("upc")
# Generate barcode and render as image
my_barcode = barcode_format(number, writer=ImageWriter())
# Save barcode as PNG
my_barcode.save(cs)
lbl1 = Label(root, text="Enter UPC-A code : ")
lbl1.grid(column=0, row=0)
e1 = Entry(root)
e1.grid(column=1, row=0, ipadx=5, padx=5, pady=5)
btn1 = Button(root, text="Calculate", command=calculated)
btn1.grid(column=2, row=0)
root.bind('<Return>', calculated)
root.mainloop()
These are the steps I took to make it work.
mkdir newdir
and cd newdir
py -m venv venv
and venv\Scripts\activate
py -m pip install --upgrade pip pyinstaller pillow python-barcode
touch main.py
main.py
import os
from tkinter import Button, Entry, Label, Tk
import barcode
from barcode.writer import ImageWriter
root = Tk()
root.minsize(400, 200)
# Add Title from Filename
title = os.path.basename(__file__)[0:-3]
root.title(title.title())
def calculated(event=None):
# Define content of the barcode as a string
# Get entry as an input for the barcode 11 char or 12 if you know the checksum digit.
number = e1.get()
# calulate the checksum and store the full barcode in the variable 'cs'
cs = barcode.UPCA(number).get_fullcode()
# print(cs)
# Get the required barcode format
barcode_format = barcode.get_barcode_class("upc")
# Generate barcode and render as image
my_barcode = barcode_format(number, writer=ImageWriter())
# Save barcode as PNG
path = os.path.join(os.getcwd(),cs) # <--- added this
my_barcode.save(path)
lbl1 = Label(root, text="Enter UPC-A code : ")
lbl1.grid(column=0, row=0)
e1 = Entry(root)
e1.grid(column=1, row=0, ipadx=5, padx=5, pady=5)
btn1 = Button(root, text="Calculate", command=calculated)
btn1.grid(column=2, row=0)
root.bind('<Return>', calculated)
root.mainloop()
I made some very minor alterations to make sure the images were written to the users current directory instead of writing them to the temporary runtime folder.
pyinstaller -F main.py
main.spec
add a tuple containing the path to the barcode fonts folder and the target path for app runtime.main.spec
a = Analysis(
['main.py'],
pathex=[],
binaries=[],
datas=[('./venv/Lib/site-packages/barcode/fonts', './barcode/fonts')], # add this
...
pyinstaller main.spec
dist\main.exe
And that's it.