How to Run a Python Script in Linux

When you have a Python script ready on a Linux system, the next step is usually straightforward: run it from the terminal. Most of the time you will use python3 script.py, but it also helps to know how to make the file executable, pass arguments, and run modules with -m.
This guide walks through the practical ways to run a Python script in Linux, using current Python 3 conventions and a few common troubleshooting steps along the way.
Syntax Overview
The most common ways to run Python code in Linux are:
python3 script.py [arguments]
./script.py [arguments]
python3 -m module_name [arguments]
python3 -c "code"If you are inside an activated Python virtual environment
, python usually points to that environment’s interpreter. Outside a virtual environment, python3 is the safer default on Linux because the plain python command may be missing or mapped differently across systems.
Quick Reference
| Command | Description |
|---|---|
python3 script.py | Run a script with the Python 3 interpreter |
python3 script.py arg1 arg2 | Run a script and pass command-line arguments |
chmod u+x script.py | Make a script executable |
./script.py | Run an executable script with a shebang |
python3 -m module_name | Run a module as a script |
python3 -c "print('hi')" | Run one short Python command |
Check That Python Is Installed
Before you run any script, make sure Python 3 is available on the system:
python3 --versionYou should see a version string similar to this:
Python 3.12.3If your system also returns Python 3 for python --version, that command will work too. For Linux instructions in general, python3 is the clearer choice and avoids ambiguity.
Run a Script with python3
The simplest method is to pass the script filename to the interpreter.
Create a file named hello.py with the following contents:
print("Hello, Linux!")Run it with:
python3 hello.pyThe output should look like this:
Hello, Linux!This is the most reliable way to run a script because it does not depend on file permissions or a shebang line.
Make the Script Executable with a Shebang
If you plan to run the file directly, add a shebang at the top of the script.
Update hello.py so it starts with:
#!/usr/bin/env python3
print("Hello, Linux!")Then make the file executable:
chmod u+x hello.pyNow you can run it like this:
./hello.pyYou should see:
Hello, Linux!For general Linux systems, #!/usr/bin/env python3 is the safer shebang because it looks up python3 in your current PATH. That makes the script more portable across distributions and environments.
Pass Arguments to a Script
Many Python scripts accept arguments from the command line. A simple way to read them is with sys.argv.
Create a file named greet.py:
import sys
name = sys.argv[1] if len(sys.argv) > 1 else "world"
print(f"Hello, {name}!")Run the script and pass one argument:
python3 greet.py LinuxizeThis prints:
Hello, Linuxize!For anything beyond one or two simple arguments, switch to argparse so you can define named options, flags, and help text cleanly.
Run a Module with python3 -m
Some Python tools are meant to be run as modules rather than as standalone script files. In those cases, use the -m option instead of pointing to a file directly.
For example, to start a simple HTTP server from the current directory, run:
python3 -m http.server 8000You can also use this pattern for Python’s virtual environment module:
python3 -m venv .venvThis form makes sure Python runs the module from the same interpreter you invoked on the command line, which helps avoid confusion when you work across multiple Python installations.
Run One-Off Python Code from the Terminal
If you only need to execute a short command and do not want to create a file first, use -c:
python3 -c "print('Hello from the terminal')"That prints:
Hello from the terminalIf you want to test a few lines interactively, start the interpreter instead:
python3Once you see the >>> prompt, you can enter Python code line by line.
Troubleshooting
python3: command not found
Python 3 is either not installed or not in your PATH. Install it from your distribution packages, then verify the installation again with python3 --version.
Permission denied when running ./script.py
The file is not executable yet. Run chmod u+x script.py and make sure the first line is #!/usr/bin/env python3.
The script uses the wrong interpreter
Run the file explicitly with python3 script.py or update the shebang to #!/usr/bin/env python3. On Linux, the plain python command may point to Python 3, Python 2, or may not exist at all, so it is better not to assume.
ModuleNotFoundError appears even though the package is installed
You are likely using a different interpreter from the one where the package was installed. Activate the correct virtual environment if needed, and install packages with python3 -m pip install package_name so pip and python3 refer to the same interpreter.
Conclusion
For day-to-day work on Linux, the two methods you will use most are python3 script.py and an executable script with #!/usr/bin/env python3. If the script depends on third-party packages, pair those patterns with a Python virtual environment
so each project keeps its own interpreter and dependencies.
Tags
Linuxize Weekly Newsletter
A quick weekly roundup of new tutorials, news, and tips.
About the authors
Raghav Maddukuri
Experienced writer with a proven track record of producing high-quality content for a variety of audiences and formats.
View author page