I'm starting coding with python, but and I want to execute my program in a terminal pop-up window in vscode. The problem is that when I open the terminal View > Terminal and type .\helloworld.py it appears a pop-up window but when the code ends, it closes immediatly. I just want that the windows stays opened
The code ends because it's just a print("Hello World")
I'm looking if there's a configuration in the terminal like a .json that do not close the windows even though the program has end if it is posible (I know I can make an input("")).
If just print("Hello World") in your code, you may try adding "console":"externalTerminal" in launch.json, then Run> Run without Debugging to get code executed in the external Terminal:
Related
Is there any way to run a Python 3 script without the terminal or the console popping out?
I tried many ways to hide the terminal at first run but even through I used .pyw extension, included a hide() function and used the --windowed flag when converting my script to an .exe through pyinstaller, the terminal still pops out for a microsecond before disappearing.
import win32console, win32gui
def hide():
window = win32console.GetConsoleWindow()
win32gui.ShowWindow(window, 0)
return True
I've read about a method in which you could run the python script through a C program to hide the terminal before execution but I would like to keep it as simple as I can.
Do you know any way to avoid the terminal flashing out when the script run?
you can hide the console window by using the .pyw extension for the file
I recently made an executable of a Python program with py2exe, and when I ran the executable, a command window appeared for a split second and then disappeared. My program itself never actually ran at all. Everything is still inside the dist folder, so I'm not sure what's actually wrong. Is there a solution for this?
If all your program does is print something and you run it by double-clicking the executable, then it simply closes the console when it finishes running. If you want the window to stay open, run your program from the command line. You can also create a batch file that runs your program and then pauses the console, so that you at least get a "press any key" before the console closes.
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")
Python version 2.7
I tried a code and it was not giving any error. When I ran the program, It showed the result only for a few milliseconds and then the window closed immediately. Is there any method to display the result permanently and to close the window only at my wish rather than it closing all by itself?
Just use an raw_input() at the end of you script. This will prompt for a string, and you can close the window then by pressing "Enter"
I am assuming that you are running it by double clicking your .py script. Your OS (Windows I assume) will then close the calling 'cmd' terminal once the script has finished, you could circumvent this by starting the terminal manually (just goto start -> execute and enter cmd) to run the script so it won't close automatically.
This happens because your code runs as fast as it can and stops when its finished.
When you open a .py program via double clicking it on a windows OS; it opens the program in a cmd window and when the code is done running it closes the window again. You only specified it to perform some calculation and then stop after that. Therefor in order to keep the program running type raw_input() or if using python 3 input() at the end of your code in order to prevent it from running all the way through until you press a key.
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