Whether you are automating system backups, processing log files, or building a small utility tool, knowing how to execute Python script in Linux is an essential skill for developers and system administrators alike. Which means linux provides a powerful command-line environment that makes running Python scripts straightforward, but the exact method depends on how you want to invoke your code and where the script is stored. This guide walks you through every practical way to run a Python script on Linux, from simple terminal commands to making your script globally executable, along with troubleshooting tips and best practices to keep your workflow smooth Worth knowing..
And yeah — that's actually more nuanced than it sounds.
Prerequisites: Installing Python on Linux
Before you can execute any Python script, you need to confirm that Python is installed on your Linux distribution. Most modern Linux systems come with Python preinstalled, but the version may vary.
Checking Python Version
Open your terminal and run the following command to see if Python 3 is available:
python3 --version
If the system returns a version number such as Python 3.11.Practically speaking, 2, you are ready to proceed. If you see a “command not found” error, you will need to install Python using your distribution’s package manager That alone is useful..
Installing Python via Package Manager
On Debian or Ubuntu-based systems, use:
sudo apt update
sudo apt install python3
On Fedora, use:
sudo dnf install python3
On Arch Linux, use:
sudo pacman -S python
Once installation completes, verify it again with python3 --version.
Method 1: Running Python Scripts Using the Terminal
The most direct way to execute Python script in Linux is by calling the Python interpreter explicitly from the command line.
Using python3 to Execute a Script
Assume you have a file named hello.py in your current directory with the following content:
print("Hello from Linux!")
To run it, type:
python3 hello.py
The interpreter reads the file, executes the code, and prints the output to your terminal.
Running Scripts with Relative and Absolute Paths
You do not have to stay in the same folder as your script. You can provide a relative path:
python3 scripts/hello.py
Or an absolute path starting from the root directory:
python3 /home/username/projects/hello.py
This method is ideal for quick tests and one-off tasks, but it requires you to type python3 every time.
Method 2: Making a Python Script Executable with Shebang and chmod
If you want to run your Python script as if it were a native Linux command—without typing python3 first—you need to make the file executable. This involves two steps: adding a shebang line and changing file permissions.
Adding the Shebang Line
The shebang is a special line at the very top of your script that tells Linux which interpreter to use. Open hello.py in a text editor and add this as the first line:
#!/usr/bin/env python3
print("Hello from Linux!")
The #!/usr/bin/env python3 line instructs the system to locate the Python 3 interpreter using the user’s environment. Save the file after adding this line.
Changing File Permissions with chmod +x
By default, new text files are not executable. You must grant execution permission using the chmod command:
chmod +x hello.py
The +x flag adds the execute bit to the file’s permission set.
Running
Running the Script as a Command
With the shebang in place and the execute bit set, simply run:
./hello.py
The .But / prefix matters. It tells the shell to look in the current directory rather than searching the directories listed in your PATH variable. Omitting it usually produces command not found, even though the file is right in front of you The details matter here..
If you would rather type the script name without the prefix, move it into a directory that is already in your PATH:
mkdir -p ~/bin
mv hello.py ~/bin/hello
echo 'export PATH="$HOME/bin:$PATH"' >> ~/.bashrc
source ~/.bashrc
Notice that the .py extension was dropped in the example above. Once a file is executable and has a shebang, Linux does not care about its extension, so hello works exactly the same way as hello.py.
Method 3: Running Code with python3 -m and python3 -c
Python also offers module-based execution, which is the preferred way to launch packages that ship a command-line entry point:
python3 -m http.server 8000
python3 -m pip install requests
The -m flag searches sys.path for the named module and runs it as __main__. For short, single-line tests, use -c:
python3 -c "print(2 ** 10)"
And to feed a script straight into the interpreter without creating a file at all:
echo 'print("No file needed")' | python3 -
Passing Arguments to Your Script
Real scripts usually need input. Anything written after the filename is collected in sys.argv:
#!/usr/bin/env python3
import sys
name = sys.argv[1] if len(sys.argv) > 1 else "world"
print(f"Hello, {name}!
```bash
python3 hello.py Linux
For anything beyond a couple of flags, switch to argparse, which gives you automatic --help output and type validation Not complicated — just consistent..
Common Errors and How to Fix Them
Permission denied– the execute bit is missing. Runchmod +x script.py.bad interpreter: No such file or directory– the shebang points to a path that does not exist, or the file was edited on Windows. Prefer#!/usr/bin/env python3and convert line endings withdos2unix script.py.SyntaxErroron valid-looking code – you are accidentally using Python 2. Always invokepython3, notpython.ModuleNotFoundError– the dependency is installed for a different interpreter or environment. Create an isolated environment withpython3 -m venv .venv, activate it viasource .venv/bin/activate, and install packages there.
Conclusion
Linux gives you several equivalent ways to run Python code, and the right choice depends on context. Calling python3 script.Because of that, py is the fastest route for experimentation and debugging, since it requires no preparation at all. Adding a shebang and the execute bit turns the same file into a self-contained command that behaves like any other program in your shell, which is the better option for tools you reuse daily.