Skip to content
Foxipa
How-topython

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.

Levels: Beginner ~12 min read

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 python3 followed 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 python3 shebang and make the file executable with chmod +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:

bash
python3 --version

If 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:

bash
cat > hello.py <<'PY'
print("Hello from Python on Linux!")
PY

You 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:

bash
python3 hello.py

Python reads the file and executes it. In this example, the terminal prints:

bash
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.

bash
python3 scripts/hello.py
python3 /home/alex/projects/myapp/hello.py

A 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:

bash
python3 greet.py Alice

Inside 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.

python
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:

python
#!/usr/bin/env python3

print("Hello from an executable Python script!")

Then make the file executable:

bash
chmod +x hello.py

Now run it with its path:

bash
./hello.py

7. 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.

bash
python3 -m venv .venv
source .venv/bin/activate
python -m pip install -r requirements.txt
python hello.py

After 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.

bash
# Ubuntu / Debian
sudo apt update
sudo apt install python3
bash
# Fedora
sudo dnf install python3
bash
# Arch Linux
sudo pacman -S python

These 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.

bash
pwd
ls

Permission 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:

bash
python3 hello.py

ModuleNotFoundError

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

  1. For a normal script, run python3 script.py.

  2. For a script elsewhere, provide its relative or absolute path.

  3. For command-line inputs, put arguments after the script filename.

  4. For a script you want to invoke directly, use a python3 shebang and chmod +x.

  5. 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.

Suggest an edit

Tell us what should be corrected, clarified, or improved. Submission will be connected to Foxipa’s contribution service later.

No information is sent anywhere yet. Your suggestion is only prepared in your browser.