Nearby lessons
5 of 108Python - Running Programs from Command Prompt
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.
main.py
Current directory changed to D:\DurgaClasses
Run a Python Program Using python
Suppose your file name is demo.py.
Run it using the following command:
main.py
The Python program starts executing.
Run a Python Program Using py
You can also run the same program using the py command.
Both commands work in the same way.
main.py
The Python program starts executing.
Example Program
main.py
Enter first number: 20 Enter second number: 10 30 10 200
Another Example
main.py
Enter first number: 100 Enter second number: 50 150 50 5000
Python 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 |