To use Python from the command line, the most common method is to run Python scripts directly using the python
command followed by your script's location.
Running Python Scripts
The primary way to execute Python code from the command line involves using the installed Python interpreter. According to the reference, to run Python scripts using the python
command, you need to open a command-line window and type in the word python followed by the path to your target script. This method works on various operating systems, including Windows, Linux, and macOS.
Steps to Run a Script
- Open Your Command Line: This is your terminal on Linux/macOS or Command Prompt/PowerShell on Windows.
- Windows: Search for "cmd" or "PowerShell" in the Start menu.
- Linux/macOS: Open the "Terminal" application.
- Navigate to Your Script (Optional but Recommended): Use the
cd
command to change the directory to where your Python script (.py
file) is saved. For example,cd Documents/PythonScripts
. This makes typing the path easier. - Execute the Script: Type
python
followed by a space and then the name of your script file.
Command Structure
Here's the basic command structure for running a script:
python your_script_name.py
If you didn't navigate to the script's directory, you'd need to provide the full path:
python /path/to/your/script/your_script_name.py
or on Windows:
python C:\Path\To\Your\Script\your_script_name.py
Examples
Let's say you have a file named hello.py
with the following content:
print("Hello, Command Line!")
To run this script from your command line:
-
If you are in the same directory as
hello.py
:python hello.py
Output:
Hello, Command Line!
-
If the script is in a different directory (e.g.,
~/Documents/Scripts/
on Linux/macOS):python ~/Documents/Scripts/hello.py
Output:
Hello, Command Line!
-
If the script is in a different directory (e.g.,
C:\Users\YourName\Desktop\Python\
on Windows):python C:\Users\YourName\Desktop\Python\hello.py
Output:
Hello, Command Line!
Understanding the python
Command
When you type python
in your command line, the operating system looks for the Python interpreter executable. If Python is installed correctly and added to your system's PATH environment variable, the system finds it and uses it to run the file specified next.
Summary Table
Component | Description | Example |
---|---|---|
python |
The command to invoke the Python interpreter. | python |
your_script.py |
The name or path to your Python script file. | my_program.py |
Full Command | Executes the script using the interpreter. | python my_program.py |
Using the Interactive Interpreter
Another way to use Python from the command line is through the interactive interpreter. Typing just python
(or python3
on some systems) without a script name opens a Python shell where you can type Python code line by line and see immediate results.
python
This will typically show you the Python version and then a prompt (>>>
):
Python 3.9.7 (default, Sep 16 2021, 08:50:36)
[Clang 12.0.0 (clang-1200.0.32.2)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> print("This is interactive")
This is interactive
>>> 2 + 2
4
>>> exit()
While the interactive interpreter is useful for testing small snippets, running scripts with python script.py
is how you typically execute larger programs from the command line.