-4

I'm trying to compile my tcountdown.py into a .exe file with pyinstaller. I've read some manuals and watched YouTube tutorials about it and all saying the same (which I did): [cmd: c:/**/*>pyinstaller --onefile -w -F tcountdown.py]. cmd has the same path as the .py's and it compiles a .exe file. But when I run tcountdown.exe it doesn't start tpopup. When I run tcountdown.py with cmd everything works well. I also compiled only tpopup into a .exe to check if something's wrong with that but this is also working without issues.

tcountdown.py:

import os
import time

time.sleep(10)
os.system('tpopup.py')

mainspript: tcountdown.py

secondary script: tpopup.py

Python version is 3.9.1

  • 3
    Please change your title to something more descriptive (and correct...). Python always does ***exactly*** what it is supposed to do. The problem is probably with what ***you are telling it to do***... – Tomerikoo Feb 21 '21 at 14:09
  • 1
    Be aware that the correct means to "run" another Python file in your program is to *import* it. Using ``os.system`` means you have two *separate* programs, which just happen to both be written in Python. Since pyinstaller packages *a* Python program, it likely does not package both. – MisterMiyagi Feb 21 '21 at 14:10
  • @Tomerikoo: sorry, i'm not good in creating titles – shiromeido Feb 21 '21 at 14:17
  • @MisterMiyagi: but when i import tpopup.py it will opened immidiately which it isn't supposed to. i want tpopup to be started later after executing tcountdown – shiromeido Feb 21 '21 at 14:20
  • Does this answer your question? [Why is Python running my module when I import it, and how do I stop it?](https://stackoverflow.com/questions/6523791/why-is-python-running-my-module-when-i-import-it-and-how-do-i-stop-it) – Tomerikoo Feb 21 '21 at 14:24
  • @shiromeido Ideally, ``tpopup`` defines a *function* that creates the popup. This allows ``tcountdown`` to import ``tpopup``, sleep, *then* call the function to create the popup. – MisterMiyagi Feb 21 '21 at 14:24
  • @MisterMiyagi: actually `tpopup` is a `window.mainloop()` and sends a shutdown in 600secs command to cmd. in the window i added buttons to snooze or abort the shutdown – shiromeido Feb 21 '21 at 14:37
  • @shiromeido That's nice, but it does not change how one idiomatically should structure a program. You can still put a ``window.mainloop()`` inside a function. – MisterMiyagi Feb 21 '21 at 14:46
  • @MisterMiyagi: i think that you think im way further in python than i am. i started learning 2 weeks ago and this is my first programm. most of the code parts are from a "python 3 for beginners" book and i'm pretty sure that the code and structure needs improvement but for know its working (in cmd only) and i'm glad about that – shiromeido Feb 21 '21 at 14:56
  • @shiromeido In that case I *strongly* recommend to focus on learning Python, not compiling it. – MisterMiyagi Feb 21 '21 at 14:59
  • @MisterMiyagi: i learned more about phyton in the last 3 days making this program than before. and isn't compiling a part (or the target) of programming? – shiromeido Feb 21 '21 at 15:04
  • @shiromeido No, there are many programming languages merely intended for interpretation. Compiling Python code to a standalone executable is not a part of the regular Python software lifecycle. – MisterMiyagi Feb 21 '21 at 15:06
  • @MisterMiyagi: in other words, python isn't supposed to end as .exe and beeing usable for normies? – shiromeido Feb 21 '21 at 15:15
  • @shiromeido Python is supposed to be run by an interpreter, which is readily available for all common operating systems. Either you treat the interpreter just like any other dependency, or you vendor (include in your executable) the interpreter. While it is possible to create standalone executables, there are some catches to this and it helps to have a grasp on how Python works in the first place. – MisterMiyagi Feb 21 '21 at 15:19

1 Answers1

0

I think you need to edit tpopup.py so that it has the setup of whatever you need to do for setup like you had:

# your setup code goes where it was here

Then, put the rest of the code into a function, say for example:

def open_popup(maybe_with_arguments):
    # rest of the code here

And then in tcountdown, you can use

import tpopup # with import, you don't need to compile tpopup - you just need to make sure it's in the same directory as tcountdown.py and pyinstaller should notice it and pack it into the .exe
import time
time.sleep(10) # waiting
tpopup.open_popup(possible_arguments) # calls the code that actually calls the popup, opening the popup AFTER the wait has been completed.
ZWORX52
  • 53
  • 1
  • 5