#๐Ÿ”’ How do I run a Python file from the command line on Windows?

54 messages ยท Page 1 of 1 (latest)

vocal creek
#

C:\Program Files\Python39\ToyCar.py
from the abspath command, I got the file above. How can I use this to run in command line? Sorry if this is a dumb question

ivory groveBOT
#

@vocal creek

Python help channel opened

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.

serene swift
#

Try either of these

#

py --version

#

python --version

#

python3 --version

vocal creek
serene swift
#

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

hybrid reef
#

You should be able to go: py C:\Program Files\Python39\ToyCar.py

serene swift
vocal creek
# serene swift If it says py is not a recognised command or something try with python or python...

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>

hybrid reef
#

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"
hybrid reef
#

Put a print("hello!") in your script, maybe up the top. See if it shows up.

vocal creek
hybrid reef
#

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?

zenith ravine
#

When you install Python, you need to check the box that says "add Python to PATH"

#

did you do it?

hybrid reef
#

I'd think py would be causing an error message if that were missed.

vocal creek
zenith ravine
hybrid reef
#

python and python3 fail, byt py seems to be doing something.

#

These days py is the usual python launcher name on windows.

zenith ravine
vocal creek
zenith ravine
#

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)

hybrid reef
#

It... might.
Your .py files should not be in the Python install area (which they seem to be). Move them into a personal area .

zenith ravine
#

It's pure nonsense that still nowadays Python devs haven't decided to let the box be auto checked on installation...

vocal creek
#

so with the print statements in the main function of my script, it prints in the command line

zenith ravine
#

do you call main ?

hybrid reef
#

Yes, that. This script defines some classes and functions but does not seem to call anything.

vocal creek
vocal creek
hybrid reef
#

What do you mean by print without print?

#

The print() calls are what produce output.

zenith ravine
#

Probably something like a variable evaluation in REPL

vocal creek
#

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

hybrid reef
#

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.

hybrid reef
vocal creek
hybrid reef
#

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.

zenith ravine
#

something like
py

import ToyCar
main()

should call your main function if you call py after moving into the ToyCar.py script directory

vocal creek
#

Thank you! I think I got it

ivory groveBOT
#
Python help channel closed using Discord native close action

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.