Python Tutorial 17: How to execute Python program from command prompt?

Before we continue with Python Tutorial, let’s discuss on how to execute a Python program from Windows Command Prompt.

 

Access to Windows command prompt

Access to windows command prompt - type CMD in Cortana
1. type CMD in Cortana
Access to windows command prompt - select "Command Prompt"
2. Select “Command Prompt”
Command Prompt

 

Run the first Python File

Suppose we have the following Python programs

 

This program runs fine under Spyder.  But to run this program in Command Prompt, type following command

Python HelloWorld.py

Exec Python from Command Prompt

 

This is the expected result

 

Exec Python from Command Prompt - Result

You may receive the following error message if it is your first time trying this:

“‘python’ is not recognized as an internal or external command,
operable program or batch file.”

This is simply because your application is not in the Path setting. You can fix this problem by referring to yesterday’s tutorial – Python Tutorial A01: How to add python application to Windows Path system Variable? and then reopen the command Prompt again.

Leave a Comment