Computer courses · Tools for technicians · Tech support Call 859-878-2615 WhatsApp Send a message

Home · Blog

Step-by-step guide

How to run Python on the command line in Windows (cmd, interactive mode and .py files)

By Gabriel ·

Short answer: to run Python on the command line in Windows, open the Command Prompt (type cmd in the Start menu), type python3 (or python / py) and press Enter to get the >>> prompt, where every line you type runs right away. To run a Python file, save your code as a .py file, go to its folder with cd and type python3 yourfile.py.

This is lesson 10 of my Python course (2021). Here you will practice both ways of using Python from the terminal: interactive mode, which is perfect for quick tests like 2+2, and running a script file, which is how real programs run. You will also see your first SyntaxError on purpose, so you understand why Python only accepts code written its own way. After the steps you will find a command cheat sheet, a troubleshooting table for errors like 'python' is not recognized, and an FAQ.

DifficultyVery easy
Time10 minutes
Tools & requirementsWindows 10 or Windows 11 computer, Python 3 installed (the video uses Python 3.9.6), Command Prompt (cmd), built into Windows, Notepad or any plain text editor, A folder for your practice files (the video uses a folder named py on the Desktop)
In this article
  1. Open the Command Prompt
  2. Start Python with python3
  3. Run your first command: 2+2
  4. See why Python gives a SyntaxError
  5. Exit the Python interpreter
  6. Go to your practice folder with cd and dir
  7. Look inside a Python file with type
  8. Clear the screen and open Notepad
  9. Write a print() line
  10. Save the file as hellotest.py
  11. Check that the file is there
  12. Run the Python file
  13. Interactive mode vs running a .py file: when to use each
  14. Command Prompt and Python commands cheat sheet
  15. python, python3 or py: which command should you use on Windows?
  16. Troubleshooting: common errors when running Python from cmd
  17. Tips to practice Python on the command line
  18. Frequently asked questions
  19. Watch the video

Step 1Open the Command Prompt

Windows 10 Start menu search showing cmd typed in the search box and the Command Prompt app as the best match, the first step to run Python on the command line
  • Click the Start button (bottom or top of the screen, depending on where your taskbar is) and type cmd.
  • Click Command Prompt in the results. A black window opens at your user folder, for example C:\Users\User>.
  • You do not need Run as administrator for this lesson; a normal window is safer for practice.

Step 2Start Python with python3

Windows Command Prompt after typing python3, showing the Python 3.9.6 version banner and the triple greater-than interactive prompt ready for Python commands
  • Type python3 and press Enter. Python prints its version (in the video Python 3.9.6, 64 bit) and shows the >>> prompt.
  • That prompt means you are now talking to Python, not to Windows. You are ready to run any command line.
  • If Windows says the command is not recognized or opens the Microsoft Store, Python is not installed or not on your PATH; see the troubleshooting table below. Depending on how you installed it, python or py may be the command that works.

Step 3Run your first command: 2+2

Python interactive prompt in Windows cmd where 2+2 was typed and Python answered 4 on the next line, showing how commands run instantly
  • Type 2+2 and press Enter. Python answers 4 immediately.
  • This is interactive mode (also called the REPL): each line runs as soon as you press Enter, which makes it great for testing small ideas.
  • Try other math like 1+1, 10*3 or 7/2 to get comfortable.

Step 4See why Python gives a SyntaxError

Command Prompt showing Python SyntaxError invalid syntax after typing 4 plus 4 in English words instead of the plus sign, with the caret pointing at the error
  • Now type 4 plus 4. Python answers SyntaxError: invalid syntax and points at the problem with a little ^.
  • Why? You are breaking the rules of the language. Python understands Python; it does not understand English, Spanish or any other human language.
  • Write it the way Python expects, 4+4, and it works every time. Reading the error line carefully is a habit that will save you hours later.

Step 5Exit the Python interpreter

Windows cmd window showing exit() typed at the Python prompt to leave the interpreter and then cd Desktop typed at the normal C:\Users\User prompt
  • To leave Python, type exit() (exit, open parenthesis, close parenthesis) and press Enter.
  • The prompt changes back to C:\Users\User>: you are talking to Windows again, not to Python.
  • Common mistake: typing Windows commands like cd or dir at the >>> prompt. They only work at the normal Windows prompt.

Step 6Go to your practice folder with cd and dir

dir command output in the Windows Command Prompt listing the Desktop contents, including the py folder used for the Python practice files
  • Type cd Desktop to move into the Desktop folder, then dir to list what is there.
  • In the video there is a folder called py for the course files, so the next command is cd py.
  • Tip: type the first letters of a name and press Tab; cmd completes the folder or file name for you.

Step 7Look inside a Python file with type

Command Prompt in the py folder showing dir with auto.py and the type auto.py command printing the file content, a single Python print line
  • The folder already has a file called auto.py. Type type auto.py and Windows shows its content without opening an editor.
  • The file has only one line: a print("...") statement with a short sentence about a blue car.
  • A .py file is just plain text. The .py extension tells Windows and Python that the text is Python code.

Step 8Clear the screen and open Notepad

Clean Command Prompt window in the Desktop py folder after cls, with the notepad command typed to open Notepad for writing a new Python file
  • Type cls to clear the screen so it is easier to read.
  • Type notepad and press Enter. Notepad opens; it is very basic, but enough to write a first Python file.
  • Later you can switch to a code editor such as Visual Studio Code, which colors your code and catches mistakes as you type.

Step 9Write a print() line

Notepad window open over the Command Prompt with the Python line print("hello I am Gabriel and I dont have a blue car") typed as the content of a new script
  • Type: print("hello I am Gabriel and I dont have a blue car") (use your own sentence).
  • Everything you want to show goes between quotation marks, and the quotes go inside the parentheses: parenthesis, quote, your text, close quote, close parenthesis.
  • Common mistakes: a missing closing quote or parenthesis, or curly “smart quotes” pasted from a word processor. Any of them gives a SyntaxError.

Step 10Save the file as hellotest.py

Notepad Save As dialog browsing to This PC, Desktop, py folder, where the new Python script is saved with the name hellotest.py
  • Click File > Save. Windows asks where to save: choose Desktop > py.
  • Name it hellotest.py. The name must end in .py because it is Python.
  • Then close Notepad and go back to the Command Prompt.

Caution: Notepad's Save as type box says Text Documents (*.txt). If your file ends up as hellotest.py.txt, save it again with Save as type: All files, or type the name in quotes: "hellotest.py".

Step 11Check that the file is there

dir listing of C:\Users\User\Desktop\py in Windows cmd showing two Python files, auto.py and hellotest.py, before running the new script
  • Type dir again. You should now see auto.py and hellotest.py in the folder.
  • On a Mac or Linux computer the command to list files is ls instead of dir.
  • If you only see hellotest.py.txt, fix the name first (see the caution in the previous step).

Step 12Run the Python file

Command Prompt showing python3 hellotest.py executed in the py folder and the output line hello I am Gabriel and I dont have a blue car printed by the script
  • Type python3 hello and press Tab so the file name completes to hellotest.py, then press Enter.
  • Python reads the file from top to bottom and prints your sentence: hello I am Gabriel and I dont have a blue car. That is how you run any Python file on a Windows computer.
  • Notice you are at the normal Windows prompt, not at >>>. Running a file is a Windows command that starts Python for you.

Interactive mode vs running a .py file: when to use each

Python on the command line works in two ways, and the video shows both:

Interactive mode (REPL)Running a script file
How to startpython3 (or python / py)python3 file.py
Prompt you see>>>normal C:\...> prompt
Code is saved?No, it is gone when you exitYes, in the .py file
Best forQuick math, testing one line, learningReal programs, anything you want to run again
How to leaveexit()The program ends by itself

A good beginner routine: test a line in interactive mode, and when it works, copy it into your .py file. When your files grow, a real editor helps a lot; see how to install Visual Studio Code on Windows for Python.

Command Prompt and Python commands cheat sheet

CommandWhere you type itWhat it does
cmdStart menu searchOpens the Command Prompt
python3 / python / pyWindows promptStarts the Python interpreter
exit()>>> promptLeaves Python (quit() also works)
cd DesktopWindows promptMoves into a folder
cd ..Windows promptGoes up one folder
dirWindows promptLists files (Mac/Linux: ls)
type auto.pyWindows promptPrints the content of a text file (Mac/Linux: cat)
clsWindows promptClears the screen (Mac/Linux: clear)
notepadWindows promptOpens Notepad
python3 hellotest.pyWindows promptRuns a Python file

The Tab key completes folder and file names, and the up arrow brings back the last command, so you rarely need to retype anything. If you like working from the terminal, the same ideas apply to other tools too, for example when you control a Blackmagic HyperDeck from the command line with Telnet.

python, python3 or py: which command should you use on Windows?

In the video, python3 starts the interpreter. On Windows the command that works depends on how Python was installed:

  • py: the Python launcher for Windows. The official Python documentation recommends py when you have more than one version, for example py -3.9, and py --list shows the versions installed.
  • python: the standard name; works when Python is on your PATH or was installed from the Microsoft Store / Python install manager.
  • python3: works on Mac and Linux, and on Windows installs that include it. The current Python docs say the python3 command on Windows is there mainly to catch the habit of typing the Mac/Linux command.

Note: if one command does not work, simply try the next one. The rest of the lesson is exactly the same: py hellotest.py runs the file the same way as python3 hellotest.py.

On a Mac you open Terminal instead of cmd and use python3, ls and cd; Linux works the same way.

Troubleshooting: common errors when running Python from cmd

What you seeLikely causeFix
*'python3' is not recognized as an internal or external command*Python not installed, or not on PATHInstall Python 3, or try py / python
Typing python opens the Microsoft StoreWindows app alias, Python not installedInstall Python, then open a new cmd window
SyntaxError: invalid syntaxCode not written in Python rules (like 4 plus 4)Check symbols, quotes and parentheses
can't open file ... No such file or directoryYou are in the wrong folder or the name is wrongcd to the folder, check with dir
File is hellotest.py.txtNotepad saved it as a text documentSave as *All files* or rename it
NameError on a wordText without quotation marksPut the text between quotes inside print()
Windows commands fail with SyntaxErrorYou are still at the >>> promptType exit() first

Tips to practice Python on the command line

  • Keep all your lesson files in one folder (like the py folder on the Desktop) so cd takes you there fast.
  • Use short file names without spaces, such as hellotest.py; spaces force you to use quotes in cmd.
  • Change your print sentence, save, and run the file again with the up arrow + Enter. Repeating this loop is the fastest way to learn.
  • Read every error message from the bottom line up: the last line says what kind of error it is.
  • When you are ready for more than one line, add a second print() below the first and run the file again; Python runs the lines in order, top to bottom.

Frequently asked questions

How do I run Python from the command line in Windows?

Open the Command Prompt (type cmd in the Start menu), type python3, python or py and press Enter. You get the >>> prompt, where each line of Python runs immediately.

How do I run a .py file in cmd?

Go to the folder of the file with cd (check it with dir) and type python3 filename.py, or python filename.py / py filename.py, then press Enter.

How do I exit Python in the Command Prompt?

Type exit() and press Enter. quit() also works, and so does Ctrl+Z followed by Enter on Windows.

Why do I get SyntaxError: invalid syntax?

Because the line is not written the way Python expects, for example 4 plus 4 instead of 4+4, or a missing quote or parenthesis. Fix the symbols and run it again.

Why does Windows say python is not recognized?

Python is not installed or its folder is not on your PATH. Install Python 3, then open a new Command Prompt and try python, py or python3.

Can I write Python code in Notepad?

Yes. Notepad is enough for small scripts; just save the file with the .py extension (use Save as type: All files so it does not become .txt).

What is the difference between python and python3?

Both start Python 3 on most modern systems. On Mac and Linux python3 is the usual command; on Windows python or the py launcher are the standard ones, and python3 works on some installs.

What command lists files on Mac or Linux instead of dir?

Use ls to list files, cd to change folders and python3 file.py to run a Python file in the Terminal.

Need help with this?

Call, text or WhatsApp me at 859-878-0554.

Watch the video

10- how to run python on a command line (Course Python 2021)

Original video: 10- how to run python on a command line (Course Python 2021)

Comments

More articles

WhatsApp Visitors today: — This month: — Total: —