How to Run a Python Script on Linux
Learn how to run Python scripts on Linux with python3, pass command-line arguments, make scripts executable, use virtual environments, and handle Ubuntu, Debian, Fedora, and Arch Linux differences.
At a glance
Before you begin
Goal
Run a Python 3 script from a Linux terminal, pass command-line arguments, make a script directly executable, and handle common distribution-specific differences.
You'll need
- A Linux system with a terminal
- Basic familiarity with files and shell commands
- A Python script, or a text editor to create one
Outcome
You can run Python scripts with python3, execute them by path, pass arguments, use an executable shebang, and choose the appropriate package-management and dependency-isolation approach for common Linux distributions.
Applies to
- Python — 3.x — Linux
The essentials
-
Use
python3followed by the script path to run a Python file from a Linux terminal. - Use a relative or absolute path when the script is not in your current directory.
-
For a directly executable script, add a
#!/usr/bin/env python3shebang and make the file executable withchmod +x. - Linux distributions package Python differently, so installation commands and the available default Python version can vary.
1. Check whether Python 3 is installed
Open a terminal and check for the Python 3 interpreter:
python3 --versionIf Python is installed, the command prints its version. The exact version depends on your Linux distribution and its current repositories.
2. Create a Python script
Create a file ending in
.py. For example, create
hello.py:
cat > hello.py <<'PY'
print("Hello from Python on Linux!")
PYYou can also create and edit the file with your preferred text editor. The important part is that the file contains valid Python code and is saved with a
.py extension.
3. Run the script with python3
If the script is in your current directory, run it with:
python3 hello.pyPython reads the file and executes it. In this example, the terminal prints:
Hello from Python on Linux!4. Run a script from another directory
The script does not have to be in your current working directory. Pass its relative or absolute path to
python3.
python3 scripts/hello.py
python3 /home/alex/projects/myapp/hello.pyA relative path is resolved from the directory where you run the command. An absolute path identifies the file from the filesystem root.
5. Pass arguments to the script
Anything after the script filename is passed to the Python program as command-line arguments. For example:
python3 greet.py AliceInside the program, Python exposes those arguments through
sys.argv. The script name is included as the first element, followed by the arguments supplied on the command line.
import sys
print("Hello,", sys.argv[1])6. Run a Python script directly as a command
Linux can also run a Python script directly when the file has a suitable shebang and executable permission. Put this at the very beginning of the file:
#!/usr/bin/env python3
print("Hello from an executable Python script!")Then make the file executable:
chmod +x hello.pyNow run it with its path:
./hello.py7. Use a virtual environment for project dependencies
Running a simple script only needs the interpreter. If the script depends on third-party packages, keep those project dependencies isolated in a virtual environment rather than modifying the system Python installation.
python3 -m venv .venv
source .venv/bin/activate
python -m pip install -r requirements.txt
python hello.pyAfter activation,
python normally refers to the interpreter inside the virtual environment. You can leave the environment with
deactivate.
8. Linux distribution notes
The command used to run an existing script is the same on most Linux distributions:
python3 script.py. What differs is how Python is installed and which version the distribution currently provides.
# Ubuntu / Debian
sudo apt update
sudo apt install python3# Fedora
sudo dnf install python3# Arch Linux
sudo pacman -S pythonThese commands install the distribution-provided Python package; they do not guarantee a particular Python minor version. Check the installed interpreter with
python3 --version afterward.
9. Troubleshoot common errors
python3: command not found
Python 3 is either not installed or its executable is not available on your PATH. Install the distribution's Python 3 package, then run
python3 --version again.
No such file or directory
Check the path and filename. Use
pwd to see your current directory and
ls to inspect its contents.
pwd
lsPermission denied when using ./hello.py
The file is probably not executable. Add the executable bit with
chmod +x hello.py or run it through the interpreter instead:
python3 hello.pyModuleNotFoundError
The script imports a package that is not available in the interpreter environment running the program. If the project uses a virtual environment, activate it before installing and running its dependencies.
Choose the right way to run the script
-
For a normal script, run
python3 script.py. -
For a script elsewhere, provide its relative or absolute path.
-
For command-line inputs, put arguments after the script filename.
-
For a script you want to invoke directly, use a
python3shebang andchmod +x. -
For third-party dependencies, use a project virtual environment instead of modifying the system Python installation.
Found an issue?
Help keep this guide accurate by suggesting an improvement.