To run a Python script from the command line, you need to open your system's command prompt or terminal, navigate to the directory where your script is saved, and then execute the script using the python
command followed by its filename.
Understanding the Basics of Running Python Scripts
Executing a Python script directly from the command line is a fundamental skill for any developer. It allows you to run your Python programs without needing an Integrated Development Environment (IDE) and is crucial for automating tasks, server-side applications, and quick testing.
Prerequisites
Before you can run a Python script from the command line, ensure you have Python installed on your system and that it's accessible through your system's PATH
environment variable.
- Python Installation: Make sure Python is installed. You can download the latest version from the official Python website.
- PATH Variable: The
PATH
environment variable tells your operating system where to find executable programs. When you typepython
in the command line, the system looks for the Python executable in the directories listed in yourPATH
.
How to Check Your Python Installation
You can verify if Python is installed and configured correctly by opening your command prompt or terminal and typing:
python --version
or
python3 --version
If Python is installed, you will see its version number (e.g., Python 3.9.7
). If you receive an error like "command not found," it means Python is either not installed or not added to your system's PATH
.
Step-by-Step Guide to Running a Python Script
Follow these steps to execute your Python script:
1. Open Your Command Line Interface
Depending on your operating system, open one of the following:
- Windows: Search for "Command Prompt" or "PowerShell" in the Start Menu.
- macOS: Open "Terminal" from Applications > Utilities.
- Linux: Open a terminal window (usually Ctrl+Alt+T or search for "Terminal").
2. Navigate to Your Script's Directory
Use the cd
(change directory) command to move into the folder where your Python script (.py
file) is saved.
Example: If your script my_script.py
is located in C:\Users\YourUser\Documents\Python_Scripts
on Windows, you would type:
cd C:\Users\YourUser\Documents\Python_Scripts
On macOS or Linux, if your script is in /Users/YourUser/Documents/Python_Scripts
, you would type:
cd /Users/YourUser/Documents/Python_Scripts
Here's a quick reference for common cd
commands:
Command | Description |
---|---|
cd folder |
Change to the specified folder within the current directory. |
cd .. |
Move up one directory level. |
cd / |
Go to the root directory (Linux/macOS). |
cd \ |
Go to the root of the current drive (Windows). |
cd ~ |
Go to the user's home directory (Linux/macOS). |
3. Execute the Python Script
Once you are in the correct directory, run your script using the python
command followed by the script's filename.
python script_name.py
- Replace
script_name.py
with the actual name of your Python file (e.g.,my_script.py
). - If you have multiple Python versions installed or your primary executable is
python3
, you might need to usepython3
instead ofpython
:python3 script_name.py
Example: If your script is named hello.py
and contains print("Hello, World!")
, the output would be:
python hello.py
Output:
Hello, World!
4. Passing Command-Line Arguments (Optional)
You can pass additional information, known as command-line arguments, to your script. These arguments can be accessed within your Python script using the sys
module.
python script_name.py arg1 arg2 "another argument"
Inside script_name.py
, you would access these with sys.argv
:
import sys
print(f"Script name: {sys.argv[0]}")
print(f"Arguments: {sys.argv[1:]}")
Alternative Methods
Using a Shebang Line (Linux/macOS)
On Linux and macOS, you can make a script directly executable by adding a "shebang" line at the very beginning of your Python file and then changing its permissions.
-
Add Shebang: At the top of
script_name.py
:#!/usr/bin/env python3 # Your Python code starts here print("This script runs directly!")
This tells the operating system to execute the script using the
python3
interpreter found in thePATH
. -
Change Permissions: Make the script executable:
chmod +x script_name.py
-
Run Directly:
./script_name.py
The
./
indicates that the script is in the current directory.
Common Issues and Troubleshooting
- 'python' is not recognized: This typically means Python is not installed or not added to your system's
PATH
environment variable. Refer to Python's official installation guide for your OS. - File Not Found Error: Ensure you are in the correct directory where the script is saved using the
cd
command, and that you have typed the script's name correctly, including the.py
extension. - Syntax Errors/Runtime Errors: These are issues within your Python code itself. The command line will usually print a traceback, indicating the line number and type of error, which helps in debugging.
By following these instructions, you can efficiently run any Python script from your command line, empowering you to integrate Python into your daily workflow.