How to make the text shell not appear when running a program - python

I'm just wondering (as im starting to make a game with pygame) is there a way to 1) Run a program that only opens a GUI and not the shell
and
2) Run the program without having to edit with idle (I'd like to do this so the game will look more professional.
I would like to just have a desktop shortcut or something that when clicked, runs only the GUI and doesnt show my code or show the shell.

If you're on windows, a quick thing could be to make a one-line batch script.
start pythonw.exe <filename>
The start keyword causes the shell to not wait for the program to finish.
pythonw vs python prevents the python terminal from appearing.
If you're on linux/mac, you could do the same with shell scripts.

Related

How to make a python file run in the background when minimized?

I made a Python program that I do NOT want to turn into .exe or anything else.
However since the program needs python.exe to run, it runs python.exe openly.
I want to know if I can add a piece of code to the program I made so that it runs in the background.
BTW, I do not want to run through cmd.I want it to be a background process automatically.
Basically, I want to add some code into my program so that as soon as it is clicked upon, it runs in background.
I found out the way to do this easily: I just renamed my file.py into file.pyw
Now, it runs without a console. Basically I typed this in the cmd:
ren file.py file.pyw

How can I start a GUI program from Python, but have the process end when the user closes the window?

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.

Python.exe opens in a new console window

I used to run Python scripts from my Windows command line, and all the prints were printed in the same console. Now something happened on my machine (Windows 10), and when I launch a Python script from the command line (i.e. open a Command Prompt and run python <my_script.py>), Windows opens a new window (titled with the absolute path of python.exe). This windows closes automatically at the end of the execution, so that I can't see the output.
How do I go back to printing output in the same command prompt window from which I run the script?
Not sure how useful this will be but I had this same problem, found this thread, and realized that the new console window was opening up when I omitted 'python' from the command.
>python myscript.py
shows the output right in the terminal where I typed the command, but
>myscript.py
opens the new console window and closes it immediately after the script runs.
It's odd but it very likely a windows setup issue as python is an exe. If memory serves windows will spawn on a > run command so checking the way python is booting will help.
Unfortunately it could be a range of issues, so some steps towards victory:
What happen when you just type python into the cmd? If it simply starts the input >>> - it means your python setup is fine. If a cmd window spawns and disappears it may be a windows permissions issue.
Try running your script with -i flag: python -i script.py. This drops you into the repl when the app completes - displaying your output.
Ensure you're using the native flavour of the cmd to test. Ensuring any command app or IDE isn't injecting a start command or weird /K (spawn new window) flag.
Hope it helps.
In my computer this was caused by Windows not knowing what program a .py file was associated with. I solved this by going to:
Control Panel -> Programs -> Default Programs -> Associate a file type or protocol with a program (Scroll down) and choose "Choose default apps by file type" Scroll down until you see ".py" and choose the correct
Python interpreter.
Simply: last row on the end of your program maybe this:
input("\nIf you whish end the program press any key ...")
...and your program wait for the key and you see your outcome

How to run a python program in the background?

How can I make a python program run in the background?
I don't need the console running since all it does is sends me emails with updates once an hour.
Is there a way to do it when I convert it to exe using py2exe?
I am using python 2.7.8.
Also, is there a way to make it open in the boot menu immediately when the computer turns on from the code?
Thank you
If you are asking strictly how to hide the console as part of the py2exe bundling, that's simple. See - Hiding command-line dialog in py2exe
If you are asking how to have a program without a GUI, just build the program in py2exe as above and run it. Unless you have created a GUI, it will run on it's own until it terminates or is terminated.

Python script displays output in command window but nothing shows in Windows

I've written a script that works great at a command prompt, but it only displays the last line if I double click on the script.py icon on my desktop.
The function in the script runs perfectly but once it finds a match it's supposed to display the output on the screen. At the end, I have an os.pause statement and THAT displays when the script is done, but nothing else displays on the screen.
I AM executing it using pythonw.exe. What else should I check?
Thank you.
pythonw supresses the console window that is created when a python script is executed. Its intended for programs that open their own GUI. Without pythonw, a python gui app would have its regular windows plus an extra console floating around. I'm not sure what pythonw does to your stdout, but os.isatty() returns False and I assume stdout/err are just dumped into oblivion. If you run a DOS command like os.system("pause"), Windows creates a new console window for that command only. That's what you see on the screen.
If you want to see your script output, you should either run with python.exe and add an input prompt at the end of the script as suggested by #GaryWalker, or use a gui toolkit like tcl/tk, Qt or wxPython.
I've used a script like before and it seemed ok to me
print("Hello world")
input("Press return to exit")

Categories