Nearby lessons
5 of 159Python - Running Programs from Command Prompt
- Learn how to run Python scripts from the command prompt or terminal
- Understand the difference between running Python interactively and via script files
- Know how to use command-line arguments with Python programs
- Troubleshoot common command-line errors when running Python
Introduction
Python programs can be run using the Command Prompt (CMD).
This method is widely used by Python developers because it is simple and fast.
After writing and saving a Python program, you can execute it directly from the Command Prompt.
Why Use Command Prompt?
Running Python programs from the Command Prompt provides several benefits.
- Run Python files directly.
- Practice Python without opening IDLE every time.
- Understand how Python works from the terminal.
- Work like professional Python developers.
Open Command Prompt
You can open the Command Prompt in different ways.
Method 1
- Press Windows + R.
- Type
cmd. - Press Enter.
Method 2
- Open the Start Menu.
- Search for Command Prompt.
- Open it.
Go to the Program Folder
Before running a Python program, move to the folder where the file is saved.
Use the cd command to change the current directory. This is an OS shell command, typed in the Command Prompt, not inside Python:
cd C:\PythonDemoRun a Python Program Using python
Suppose your file name is demo.py.
Run it using the following command. This is typed in the OS shell (Command Prompt), not inside Python:
python demo.pyRun a Python Program Using py
You can also run the same program using the py command.
Both commands work in the same way. This is an OS shell command, typed in the Command Prompt:
py demo.pyExample Program
Using Command-Line Arguments
You can also pass arguments to a Python script from the command line using the sys module.
Save this code in a file named greet.py:
import sys
name = sys.argv[1] if len(sys.argv) > 1 else "World"
print("Hello,", name)Then run it from the Command Prompt:
python greet.py RajeshThe program prints:
Hello, RajeshPython Shell vs Python File
| Python Shell | Python File (.py) |
|---|---|
| Runs one or two lines of code. | Runs complete Python programs. |
| Useful for testing syntax. | Useful for projects and large programs. |
| Does not save the program. | Programs can be saved and reused. |
| Good for quick calculations. | Good for real applications. |
Advantages of Running from Command Prompt
- Fast program execution.
- Easy to run saved Python files.
- No need to open IDLE every time.
- Commonly used in real-world development.
- Works well with automation and scripts.
Common Beginner Mistakes
1. Running the command from the wrong folder.
Always move to the folder where your Python file is saved.
2. Using the wrong file name.
Make sure the file name is correct before running it.
3. Using the wrong file extension.
Python programs must always be saved with the .py extension.
Important Commands
| Command | Purpose |
|---|---|
python |
Start Python Shell. |
py |
Start Python Shell. |
python filename.py |
Run a Python program. |
py filename.py |
Run a Python program. |
exit() |
Close the Python Shell. |
Key Points
- Save the Python program before running it.
- Always use the
.pyextension. - Move to the correct folder using the
cdcommand. - Both
pythonandpycan run Python programs. - Command Prompt is commonly used in professional Python development.
Quick Summary
| Topic | Description |
|---|---|
| Terminal | Command Prompt (CMD) |
| Change Directory | cd folder_name |
| Run Program | python filename.py |
| Alternative Command | py filename.py |
| Python File Extension | .py |
- Use the `python` or `python3` command followed by the script filename to run a .py file
- The command prompt gives more control than IDLE for running and testing programs
- Always navigate to the correct directory before running a Python script
- Common errors like "command not found" usually mean Python is not added to the system PATH
- Command-line execution is essential for automation scripts and server-side programs