#๐ How do I run a Python file from the command line on Windows?
54 messages ยท Page 1 of 1 (latest)
@vocal creek
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
what does version stand for?
It tells what version of python you have. But we are running it to find if it's py or python or python3 command to call the python interpreter in your system. It varies if you have set any alias before
Run those 3 commands and lmk which one works
You should be able to go: py C:\Program Files\Python39\ToyCar.py
If it says py is not a recognised command or something try with python or python3 instead
I get these when i put them in
C:\Windows\System32>python C:\Program Files\Python39\ToyCar.py
Python was not found; run without arguments to install from the Microsoft Store, or disable this shortcut from Settings > Apps > Advanced app settings > App execution aliases.
C:\Windows\System32>python3 C:\Program Files\Python39\ToyCar.py
Python was not found; run without arguments to install from the Microsoft Store, or disable this shortcut from Settings > Apps > Advanced app settings > App execution aliases.
py C:\Program Files\Python39\ToyCar.py
after the py one, it just went to a new line and got: C:\Windows\System32>
Oh: there's a space in the path. You might need to put the whole thing in double quotes or something. Eg:
py "C:\Program Files\Python39\ToyCar.py"
Oh, odd. Maybe it ran your script.
Put a print("hello!") in your script, maybe up the top. See if it shows up.
same thing happened when i put in print("hello!") in my script, just went to a new line with C:\Windows\System32>
1: try it with the double quotes around the script pathname
2: maybe post the script content - what's it do? What's in it?
When you install Python, you need to check the box that says "add Python to PATH"
did you do it?
I'd think py would be causing an error message if that were missed.
I'm not sure, but I ran one of my files before through the command line, so I'm assuming I do. I just don't remember how to do it
I'm assuming no, since you can't invoke python in terminal
python and python3 fail, byt py seems to be doing something.
These days py is the usual python launcher name on windows.
Can you check by reinstalling from official installer just to be sure?
would that mess up my files?
no
I suggest you uninstall all python versions
and then install from official installer, checking the box to add python to path (leave the rest to default in personalized installation mode)
It... might.
Your .py files should not be in the Python install area (which they seem to be). Move them into a personal area .
It's pure nonsense that still nowadays Python devs haven't decided to let the box be auto checked on installation...
here is my code
so with the print statements in the main function of my script, it prints in the command line
do you call main ?
Yes, that. This script defines some classes and functions but does not seem to call anything.
yes I just didn't post the whole code
Well that sounds correct.
is there a way to print out the output without the print statements? I need it so that it runs by calling the functions in command prompt
Probably something like a variable evaluation in REPL
Sorry for my poor python vocab.
I mean if i type in this:
>>> print( c1 )```
into the command line i should get my output instead of having to have the whole main function in my script to get the output
The >>> prompt is Python's interactive prompt. The REPL which Steve refers to (Read Evaluate Print Loop).
Just typing py should enter Python's interactive mode.
You could put a print() call outside the main function, right down the bottom.
so if it is ran from REPL in the command line, the script will run like in interactive mode?
From inside python (the >>> prompt) you can basicly only import your script.
For that, you need to cd to the directory holding your script, usually.
At the CMD prompt: cd C:\the\path\to\the\script\folder
At the >>> prompt: import os; os.chdir(r'C:\the\path\to\the\script\folder')
After that you could: import ToyCar at the >>> prompt.
Then access things from your script.
something like
py
import ToyCar
main()
should call your main function if you call py after moving into the ToyCar.py script directory
Thank you! I think I got it
This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.