I can't seem to find the right google search to figure out something that should be easy.
I am using wxPython to create a GUI, and saving the Python files as .pyw so I don't have a console. Then I am importing another .py file into the main window when a user does an action. Doing so created the pythonw.exe instance that won't close. To be clear, the .pyw that opens if I only open the main console will clear from my processes if I don't open/insert the other .py file from the first file while running.
When I use the program everything is perfectly fine, but when I use the Windows "X" button to close I still get pythonw.exe in my processes, and the file I am printing errors to is locked due to something else using it(the pythonw.exe). What code do I need to use to make sure that python is exiting completely? It also seems to stay whenever I build in a file - exit as well, and it is only staying on the processes if I insert the other module(only done with user input)
Also this may be obvious but if I open the imported file manually it has the same behavior, as in the python.exe closes when I close out of the file.
If you want to exit the application you can do this like this:
import os
os._exit(0)
This works when the normal exit() or reaching the end of the main file does not work becaus of background threads running.
But so you are ending killing all running threads. You should look into the code of the imported file which causes the problem to see if it has threads.
Related
I want to write a Python script which will start a GUI program (as in, run a binary program with subprocess.run or os.system or something). The script should not block until the program is done, it should start it and then keep running.
I'm doing this on Ubuntu.
I tried subprocess.Popen, but if I run, say subprocess.Popen("gedit"), I get strange behavior. If I open the Ubuntu system monitor (process manager), I can see that the gedit process appears when I run the script, and the gedit window itself opens. But if I close the window, the process doesn't end in the system monitor. The process stays there until my python script exits.
How can I get the behavior I want? The only thing I can think of right now is to just call subprocess.run in a different Python thread, is that the only thing I can do?
Try using subprocess.call. This has worked for me before.
subprocess.call(['command', 'arguments'])
The program should end when the window is closed.
You have to kill the subprocess you've created before you exit the program.
Try this.
I am using Windows 7. I installed Python and everything is working fine except the fact that as I am saving python scripts on desktop or anywhere, none of them are opening.
.py files are closing in a blink!
.pyw files are not even responding.
Any help appreciated. I have already uninstalled and reinstalled the software suite but the problem still persists.
Here are the pictures of the icons.
What happens when you run a .py file is that a cmd.exe window is opened where Python runs your script. But this window will close as soon as your script finishes.
What you can do in this case:
Add raw_input() (python2) or input() to the end of your script to make it pause for input. That wat you can read any output before the window closes.
Open a cmd window and run your scripts from that.
With regard to .pyw files, they are themselves responsible for creating a window. So unless those scripts create a GUI you probably won't see anything.
Basically it's all in the title, when I run the code from a console (on Windows) the child process runs without opening another console, but when I run the code from the cx_freeze'd app another console opens.
I found this old thread where was suggested to use FreeConsole(), it will flash the console on screen for a blink but I can eventually live with it, unfortunately if i understood correctly it should be called from the child process.
http://twistedmatrix.com/pipermail/twisted-python/2007-February/014738.html
I also found this ticket (7yo) on a re-factoring of the whole spawnProcess on windows but apparently it never happened:
http://twistedmatrix.com/trac/ticket/2415
I have no control over the code of the child process, so doing something there is unfortunately not an option, but even if I did the process I'm spawning it's a console app and I believe FreeConsole() could not be called there or the process will terminate.
This is possibly a bug in Twisted, but possibly a bug in cx_Freeze.
What happens when you run the code from the GUI with Python, but without cx_Freeze involved? You should be able to test this if you have Python installed by simply putting your code into a .pyw file and double-clicking on it in Explorer.
If this still pops up a console window when you run your subprocess, then this is totally a bug in Twisted and you should file it as such. Eric's answer in that mailing list message is wrong; if you want to spawn processes with spawnProcess they definitely shouldn't be popping up random console windows.
If the click-on-a-.pyw launching method doesn't cause a console window to pop up, then it's probably something about the way cx_Freeze has constructed your executable and you might want to look at it.
On UNIX, we have a platform-specific usePTY, so it makes sense that we could expand spawnProcess with a platform-specific useConsole that would do something analogous for Windows. This later message in the thread suggests an implementation strategy, so please file a ticket. The let's-redo-everything ticket is too ambitious to fix this one issue.
I have created a script in python and saved it as .pyw file so that I don't need console to execute the script. I am wondering how to stop the execution once it is running. What I am doing right now is opening the IDLE and closing it and it seems to work. I am sure there is a better way of doing this. Can somebody advise me about this?
As it says in the docs
The Python installer automatically associates .py files with python.exe so that a double-click on a Python file will run it as a script. The extension can also be .pyw, in that case, the console window that normally appears is suppressed.
The .pyw extension is really meant to be used by Python GUIs. Since they create their own windows, the console window isn't required. If a .pyw script doesn't create any GUI windows, killing the python process is the only way to stop execution.
If you don't want to use the console to run your script (using the .py extension), just double-click on it.
So I want to make a Python file that runs all the code within, but the window is invisible. So the user won't see the window in the task bar, or really anywhere on his screen.
How would I do something like that?
To make a script not open the terminal window change the extension of your script to .pyw which will cause the script to be executed by pythonw.exe by default. This suppresses the terminal window on startup.
If you would like all scripts to open like this you should read up on Executing Scripts.
Maybe you'd want to use start pythonw.exe