Running Python in Linux gives you access to a vast ecosystem of libraries and system-level control. If you’re wondering How To Run Python In Linux, you’re in the right place—this guide walks you through every step, from checking your installation to running scripts and troubleshooting common issues. Whether you’re a beginner or a seasoned developer, these instructions will get you coding fast.
Python is one of the most popular programming languages, and Linux is its natural home. Most Linux distributions come with Python pre-installed, but you might need to update it or install additional tools. Let’s start with the basics and build up from there.
Checking If Python Is Installed
Before you do anything else, check if Python is already on your system. Open your terminal—usually found in your applications menu or by pressing Ctrl+Alt+T.
Type this command and press Enter:
python3 --version
If Python is installed, you’ll see something like Python 3.10.12. If not, you’ll get an error message. Don’t worry—we’ll cover installation next.
You can also check for Python 2, though it’s outdated:
python --version
Most modern systems use Python 3, so stick with that.
Installing Python On Linux
If Python isn’t installed, you can add it quickly using your package manager. The exact command depends on your distribution.
For Ubuntu And Debian-Based Systems
Run these commands in the terminal:
- Update your package list:
sudo apt update - Install Python 3:
sudo apt install python3 - Verify installation:
python3 --version
For Fedora And Red Hat-Based Systems
Use the dnf package manager:
- Install Python 3:
sudo dnf install python3 - Check the version:
python3 --version
For Arch Linux
Use pacman:
- Install Python:
sudo pacman -S python - Verify:
python --version
After installation, you’re ready to run Python scripts. If you need the latest version not in your repos, consider compiling from source or using a tool like pyenv.
How To Run Python In Linux: The Basics
Now that Python is installed, let’s get to the core of this guide: actually running code. There are several ways to do this, and each has its use case.
Using The Python Interpreter Interactively
The simplest way is to start the interactive interpreter. Just type python3 in your terminal and press Enter. You’ll see a prompt like >>>. Here you can type Python commands and see results immediately.
Example:
>>> print("Hello, Linux!")
Hello, Linux!
Type exit() or press Ctrl+D to leave the interpreter.
Running A Python Script File
For real projects, you’ll save your code in a file with a .py extension. Create a file called hello.py with this content:
print("Hello from a script!")
Then run it from the terminal:
python3 hello.py
You’ll see the output printed to your screen. This is the most common way to run Python in Linux.
Making Scripts Executable Directly
You can run a Python script like any other program by making it executable. First, add a shebang line at the top of your script:
#!/usr/bin/env python3
print("Running as an executable!")
Then give it execute permissions:
chmod +x hello.py
Now run it directly:
./hello.py
This is handy for scripts you use often.
Setting Up A Virtual Environment
When you work on multiple projects, you’ll want to isolate their dependencies. Virtual environments let you do that without messing up your system Python.
Creating A Virtual Environment
Navigate to your project folder and run:
python3 -m venv myenv
This creates a folder called myenv with a clean Python installation.
Activating The Environment
To use it, activate it with:
source myenv/bin/activate
Your terminal prompt will change to show the environment name. Now any Python or pip commands you run will use this isolated setup.
Deactivating
When you’re done, type:
deactivate
Virtual environments are essential for avoiding dependency conflicts. Always use them for non-trivial projects.
Installing Python Packages With Pip
Pip is Python’s package manager. It lets you install libraries like NumPy, Flask, or Requests. First, ensure pip is installed:
sudo apt install python3-pip
Then install a package:
pip3 install requests
To list installed packages:
pip3 list
Always use pip within a virtual environment to keep things clean.
Running Python Scripts With Arguments
You can pass arguments to your Python scripts from the command line. This is useful for automation and data processing.
Create a script called args.py:
import sys
print("Arguments:", sys.argv)
Run it with some arguments:
python3 args.py hello world 123
You’ll see the list of arguments printed. The first item (sys.argv[0]) is always the script name.
Using Python In Shell Scripts
You can embed Python code directly in bash scripts using a heredoc. This is handy for quick tasks.
Example bash script:
#!/bin/bash
python3 << EOF
print("This is Python inside bash!")
EOF
Save it, make it executable, and run it. Python executes the code between EOF markers.
Common Errors And Fixes
Even experienced users hit snags. Here are typical issues and how to resolve them.
Command Not Found: Python3
If you see bash: python3: command not found, Python isn't installed or not in your PATH. Reinstall or check your installation.
Permission Denied
When running a script directly, you might get Permission denied. Use chmod +x script.py to fix it.
ModuleNotFoundError
This means a required package isn't installed. Use pip3 install package_name to add it.
SyntaxError With Print
If you're using Python 2 syntax (like print "hello"), you'll get an error in Python 3. Always use parentheses: print("hello").
Using An IDE Or Text Editor
While the terminal is fine, an IDE can boost your productivity. Popular choices for Linux include:
- VS Code: Free, extensible, with great Python support
- PyCharm: Full-featured IDE, community edition is free
- Vim/Neovim: Lightweight, powerful for terminal lovers
- Geany: Simple and fast
To run a script from VS Code, just open the file and press Ctrl+F5. The integrated terminal will show output.
Running Python In The Background
For long-running scripts, you might want to run them in the background. Add an ampersand at the end:
python3 long_script.py &
You can also use nohup to keep it running after you log out:
nohup python3 long_script.py &
Output goes to nohup.out by default.
Debugging Python Scripts
When things go wrong, use Python's built-in debugger. Insert this line in your script:
import pdb; pdb.set_trace()
When execution hits that line, you'll enter interactive debugging mode. You can inspect variables, step through code, and fix issues.
Alternatively, run your script with the debugger from the start:
python3 -m pdb script.py
Automating Python Scripts With Cron
Linux's cron scheduler lets you run Python scripts at set intervals. Edit your crontab:
crontab -e
Add a line like this to run a script every day at 6 AM:
0 6 * * * /usr/bin/python3 /home/username/script.py
Make sure to use full paths for both Python and your script.
Using Python With Systemd
For more robust automation, create a systemd service. This is great for web servers or bots.
Create a file at /etc/systemd/system/myscript.service:
[Unit]
Description=My Python Script
[Service]
ExecStart=/usr/bin/python3 /home/username/script.py
Restart=always
User=username
[Install]
WantedBy=multi-user.target
Then enable and start it:
sudo systemctl enable myscript.service
sudo systemctl start myscript.service
Working With Python Versions
Sometimes you need multiple Python versions. Tools like pyenv make this easy.
Install pyenv:
curl https://pyenv.run | bash
Then list available versions:
pyenv install --list
Install a specific version:
pyenv install 3.11.0
Set it globally or per project:
pyenv global 3.11.0
This is a lifesaver when working on legacy projects.
Optimizing Python Performance On Linux
For compute-heavy tasks, you can speed things up. Use numpy for numerical work, or compile with Cython. Also, consider using multiprocessing to leverage multiple CPU cores.
Example with multiprocessing:
from multiprocessing import Pool
def square(x):
return x * x
with Pool(4) as p:
print(p.map(square, [1, 2, 3, 4]))
This runs the function in parallel across 4 processes.
Frequently Asked Questions
How do I run a Python script in Linux terminal?
Navigate to the script's directory and type python3 script.py. Replace script.py with your file name.
What is the difference between python and python3 commands?
On many systems, python refers to Python 2, while python3 is for Python 3. Always use python3 to avoid confusion.
Can I run Python without installing it?
No, Python must be installed. However, you can use online interpreters or cloud environments temporarily.
Why does my script run but show no output?
Check if your script has print statements. Also, ensure you're not running it in the background without redirecting output.
How do I run a Python file as a program?
Add a shebang line (#!/usr/bin/env python3), make it executable with chmod +x, then run it with ./filename.py.
Conclusion
Running Python in Linux is straightforward once you know the commands. Start with the interpreter for quick tests, use scripts for real work, and leverage virtual environments for clean development. The terminal is your friend—practice these steps, and you'll be automating tasks in no time. Remember to check your Python version, install packages with pip, and use cron or systemd for automation. With these skills, you can build anything from simple scripts to complex applications on Linux.