I wrote a very ordinary python program. Print numbers from 0 to 50. But the work of the program in the Windows Terminal was absolutely incomprehensible to me.
Although in the settings there is "Command Prompt" as the default terminal application, at startup .py from the file manager (double-click on it), the program opens in Windows Terminal, but not in "Command Prompt", I don't understand what to do with it.
When running the program through the executable file .py I have encountered this behavior:
Only the numbers from 21 to 29 that were placed in the terminal window and 21 empty lines before them were displayed on the terminal screen.
And when changing the size of the terminal window, even more incomprehensible things happened to me.
But when I run the file directly from the terminal, then everything is fine.
Operating system - Windows 11. File .py run in the default Windows Terminal. Python code:
for i in range(50):
print(i)
Result of code execution:
Terminal when resizing:
Please help me understand the operation and device of the Windows Terminal when opening .py file. And how it can be fixed, what to do about it. I am grateful in advance.
Default terminal - “Command Prompt”:
But it's not this program that starts, but just Windows Terminal.
But if I put input() at the beginning of the program, then everything works fine:
input()
for i in range(50):
print(i)
Is it possible to solve this somehow without input()?
On the left is a not working program running through an executable file, on the right is a program running through a Command Line:
Related
I have created a Python script for which I don't want to see the console pop-up every time it's being ran. I tried the pythonw.exe + .pyw approach, and while the script works just fine, the console still pops-up regardless.
.bat file content:
"C:\Users\mariu\anaconda3\pythonw.exe" "C:\Users\mariu\test.pyw" pause
And this is the inevitable console:
Is there something am I missing?
I had the same issue and moved to powershell script, since my tasks needed to start in parallel, and batch script seems not to behave like that, even with pythonw.exe
A .ps1 with the lines below should do the work
& "C:\Users\mariu\anaconda3\pythonw.exe" "C:\Users\mariu\test.pyw"
exit
I am struggling with an apparently obvious issue. I've been practising python and I have a few scripts and I am trying to run them from windows 10 execute command (WIN + R).
I am doing this by creating a BATCH file of the script, and in theory the file should run with a simple "py script" in the WIN+R window. But it does not, it simply flashes the CMD window for a sec and then disappear.
Everything seems to be set up correctly, I can run the script from CMD, BATCH file are OK (they run with a double click, I included the #pause at the end), the folder where BATCH are is in the env. variables PATH.
So i really don't get what is wrong. Note that If I type the full path in the WIN+R window it works, but that takes quite some time..
Any hints?
A BIG thank you :)
EDIT: thanks for the replies, here is the batch:
#py script.py
#pause
It works on double click.
Ctrl + R => opens run window.
Type in "cmd"
If you have python installed with Path.
python <yourscript.py>
should run just fine.
Otherwise,
Run as below.
path/to/python/python.exe <yourscript.py>
Hope you did the same as above. If the bat runs and closes in a flash. That is not an issue. It happens.
Have a sleep in your bat as well. If you're running on bat.
Add below after your python script execution.
It will make your bat script wait for a thirty seconds before closing and you can verify your stdout.
timeout /t 30
On Windows, executing Python scripts directly, i.e. without calling python before, can be tricky. Often Windows will just open your default Python editor to show you the file, because the .py extension is registered to this editor.
see this post for more info:
How to execute Python scripts in Windows?
You should create a batch file as follows...
#py -3 E:\FOLDER\SUBFOLDER\FILE.py %*
#pause
I have both python 2 and python 3 so I use py -3 as for me py will run Python 2
The ending %* is important
I've programmed a bit in Python for the last few days and everything has worked fine up until yesterday. Whenever I try to run a script now the shell only responds with the message RESTART and a path to where my script is stored. Here's an image:
I don't know how to fix this and can't find any solutions to it. When I'm writing the code directly into the shell everything works fine but this error only appears when I try to run a script from another file.
Thanks in advance
Except for the RESTART line, the behavior you see has nothing to do with IDLE. Run any program (without IDLE) that does not print anything with the '-i' option and you will see the same thing -- a '>>> ' prompt.
C:\Users\Terry>type testprog.py
5+5
C:\Users\Terry>python -i testprog.py
>>> quit()
C:\Users\Terry>
IDLE runs a editor window code the same as if it were run in a console (Command Prompt on Windows) with 'python -i'.
That's because your testprog.py doesn't actually output anything. Try changing it to print(5 + 5) or something similar, currently it only computes 5+5 and then it exits.
I'm trying out some data science tutorials with python and can't get print to, well, print! when I run a .py in windows command shell or powershell. Print does work when I use the interpreter, but I have to type it in line by line (I'm not seeing how to run a .py as a file in the interpreter). I'm attaching snips of the file, and a snip of me running in the interpreter. I tried to attach snips of what happens when I run in command shell and powershell, but apparently I need at least 10 reputation points before I can post more than 2 links. Admittedly, those snips weren't interesting; there is no error and nothing printed. It just runs and returns to the prompt.
Also, as a test, I saved a .py file that simply does print ("Hello") and it does print correctly in windows command prompt and powershell.
Thanks in advance for any help!
Casie
PY script
Snip From Python Shell
Is that image from the IDLE console? To run the script you are editing, use menu Run > Run Module, or F5. Every python GUIs has an equivalent feature.
To run an arbitrary script from inside the commandline interpreter, say mywork.py: As long as it's in the working directory that you were in when you started the interpreter, you run it (once) by typing import mywork. (As you can see, its name must be a python identifier.)
Edit: You'd get to the bottom of this a lot quicker if you'd put print("Hello, world") in a script by itself and run it. From over here it looks like it would print just fine, proving there's nothing wrong with your python interpreter.
Your script has a bug, though: As soon as you enter the function random_kid(), you leave it again since you return a value on the first line. All those print statements are never executed. Why you think it works differently with %run I can't say, but for sure this function cannot print any output.
So I am trying to debug some Python code that I am running, because for some reason when I run the code in a command line it works perfectly, but when I make it a .py file and run it that way it fails.
The output is in a Windows Commander window, and it disappears before I can read it.
Is there a way to see what this output is?
Open a windows commander, and then go to the dir where a.py is, run "python a.py", it will not disappear!