0

I have the following question for Python 3.8.8. I have the following file called test2.py (I know the names of my files aren't great, sorry for that):

# test2.py
from tkinter import *
from tkinter import ttk

window = Tk()
tab_control = ttk.Notebook(window)
tab1 = ttk.Frame(tab_control)
tab2 = ttk.Frame(tab_control)

tab_control.add(tab1, text='First tab')
tab_control.add(tab2, text='Second tab')
lbl1 = Label(tab1, text= 'First label')
lbl1.grid(column=0, row=0)
lbl2 = Label(tab2, text= 'Second label')
lbl2.grid(column=0, row=0)

tab_control.pack(expand=1, fill='both')

window.mainloop()

This code works just fine and if I type pyinstaller --onefile --noconsole test2.py the output is as expected (a working standalone file).

However, I now change my test2.py code to:

# test2.py
from tkinter import *
from tkinter import ttk
from test import *

window = Tk()
tab_control = ttk.Notebook(window)
tab1 = ttk.Frame(tab_control)
tab2 = ttk.Frame(tab_control)

tab_control.add(tab1, text='First tab')
tab_control.add(tab2, text='Second tab')
lbl1 = Label(tab1, text= text1)
lbl1.grid(column=0, row=0)
lbl2 = Label(tab2, text= text2)
lbl2.grid(column=0, row=0)

tab_control.pack(expand=1, fill='both')

window.mainloop()

where test.py (again not a great name, sorry) is in the same folder as test2.py and reads:

# test.py
text1 = 'First label'
text2 = 'Second label'

When I execute test2.py in my terminal it works perfectly, but pyinstaller --onefile --noconsole test2.py now gives a corrupt file (it is being generated, but when I try to execute it I get an error message saying "Fatal error detected" as title and "Failed to execute script test2" as message). Can somebody help me with this? Many thanks in advance!

On another note, how could I fix this problem if test.py was in a subfolder? So for example test2.py is in a folder called Downloads, and test.py is in a folder called Subfunctions which is in turn a folder in Downloads. Again many thanks!

Chris
  • 1
  • 1
  • You can try ```-p test.py``` by [this post](https://stackoverflow.com/questions/21556081/pyinstaller-2-1-import-custom-package) – Muhammed YILMAZ Oct 25 '21 at 08:36
  • 2
    Try renaming `test.py` to `test1.py` because `test` is a built-in module. Then change `from test import *` to `from test1 import *` in `test2.py`. – acw1668 Oct 25 '21 at 09:29
  • @MuhammedYILMAZ this still doesn't work: in my window I get a message saying ``` (line 1) Traceback (most recent call last): (line 2) File "test2.py", line 12, in (line 3) NameError: name 'text1' is not defined (line 4) [10992] Failed to execute script test2 ``` before the window disappears in a small fraction of a second. – Chris Oct 25 '21 at 09:30
  • @acw1668 This does work, thank you! I'll try to find myself how to import multiple files from subfolders (and update it here as soon as I find it). – Chris Oct 25 '21 at 09:39

0 Answers0