How To Run Python On Linux – Choosing Between Python2 And Python3

Setting up Python on Linux involves installing the interpreter and verifying your environment variables. If you are wondering how to run python on linux, you are in the right place. This guide will walk you through every step, from installation to running your first script.

Python is one of the most popular programming languages today. It is used for web development, data science, automation, and more. Linux, being a developer-friendly operating system, makes running Python straightforward.

In this article, you will learn the exact steps to get Python running on your Linux machine. We will cover installation, writing scripts, and troubleshooting common issues.

How To Run Python On Linux

Before you can run Python, you need to ensure it is installed on your system. Most Linux distributions come with Python pre-installed. However, the version might be older. You can check by opening a terminal.

Open your terminal application. You can usually find it in your system menu or by pressing Ctrl+Alt+T. Once open, type the following command:

python --version

If Python is installed, you will see a version number like Python 3.10.12. If not, you will get an error message. Do not worry; installing Python is simple.

Check Python Installation

First, verify if Python is already present. Run these commands in your terminal:

  • python --version – checks for Python 2 or 3
  • python3 --version – specifically checks for Python 3

If you see a version number, you are good to go. If not, proceed to the next section.

Install Python On Linux

Different Linux distributions use different package managers. Here is how to install Python on the most common ones:

Ubuntu And Debian

For Ubuntu or Debian-based systems, use apt:

  1. Update your package list: sudo apt update
  2. Install Python 3: sudo apt install python3
  3. Install pip (Python package manager): sudo apt install python3-pip

Fedora And Red Hat

For Fedora or RHEL-based systems, use dnf:

  1. Update packages: sudo dnf update
  2. Install Python 3: sudo dnf install python3
  3. Install pip: sudo dnf install python3-pip

Arch Linux

For Arch Linux, use pacman:

  1. Update system: sudo pacman -Syu
  2. Install Python 3: sudo pacman -S python
  3. Install pip: sudo pacman -S python-pip

Verify Environment Variables

After installation, ensure Python is in your PATH. This allows you to run it from any directory. Check by typing:

which python3

This should return a path like /usr/bin/python3. If not, you may need to add it manually. Edit your .bashrc or .bash_profile file and add:

export PATH=$PATH:/usr/bin/python3

Then reload with source ~/.bashrc.

Running Python Scripts

Now that Python is installed, you can run scripts. There are several ways to execute Python code on Linux.

Using The Python Interpreter

You can run Python interactively. Type python3 in the terminal. This opens the Python shell. You can type code directly:

print("Hello, Linux!")

Press Enter to see the output. To exit, type exit() or press Ctrl+D.

Running A Python File

Create a Python file with a .py extension. Use any text editor like nano or vim. For example:

  1. Open terminal and type: nano hello.py
  2. Write your code: print("Hello from Python!")
  3. Save and exit (in nano: Ctrl+O, then Ctrl+X)
  4. Run the file: python3 hello.py

You will see the output printed in the terminal.

Making Scripts Executable

You can run Python scripts like any other program. Add a shebang line at the top of your file:

#!/usr/bin/env python3

Then make the file executable:

chmod +x hello.py

Now run it directly: ./hello.py

Using Virtual Environments

Virtual environments help manage dependencies for different projects. They are essential for serious development.

Create A Virtual Environment

Navigate to your project folder. Run:

python3 -m venv myenv

This creates a folder called myenv containing a isolated Python installation.

Activate The Environment

Activate it with:

source myenv/bin/activate

Your terminal prompt will change to show the environment name. Now any Python packages you install will be isolated.

Deactivate

When done, type deactivate to return to the global Python.

Common Issues And Solutions

Sometimes things go wrong. Here are frequent problems when learning how to run python on linux.

Command Not Found

If you see command not found: python, try python3 instead. On some systems, python refers to Python 2, which may not be installed.

Permission Denied

When running a script with ./, you may get a permission error. Use chmod +x filename.py to fix this.

Module Not Found

If you import a module and get an error, you likely need to install it. Use pip: pip3 install module_name.

Running Python In The Background

For long-running scripts, you may want to run them in the background. Use the & symbol:

python3 script.py &

This runs the script in the background. You can also use nohup to keep it running after you log out:

nohup python3 script.py &

Using Cron To Schedule Python Scripts

You can automate Python scripts with cron. Edit your crontab:

crontab -e

Add a line to run a script daily at 2 AM:

0 2 * * * /usr/bin/python3 /home/user/script.py

Save and exit. The script will run automatically.

Debugging Python Scripts

When your script does not work, debugging is essential. Python has a built-in debugger called pdb.

Using Pdb

Insert this line in your code to set a breakpoint:

import pdb; pdb.set_trace()

Run the script normally. Execution will pause at the breakpoint. You can inspect variables and step through code.

Print Debugging

Simple print() statements are often enough. Add them to see variable values at different stages.

Working With Python Versions

Sometimes you need multiple Python versions. Tools like pyenv help manage them.

Install Pyenv

Install pyenv using the installer script:

curl https://pyenv.run | bash

Follow the instructions to add pyenv to your shell.

Install A Specific Python Version

List available versions:

pyenv install --list

Install a version, for example:

pyenv install 3.9.7

Switch Versions

Set a global version:

pyenv global 3.9.7

Or set a local version for a project:

pyenv local 3.10.0

Using IDEs And Text Editors

While the terminal is powerful, many prefer graphical tools. Popular options include:

  • VS Code – free, with Python extensions
  • PyCharm – dedicated Python IDE
  • Vim – terminal-based editor
  • Geany – lightweight and fast

These tools provide syntax highlighting, debugging, and integrated terminals.

Running Python In Docker

Docker containers are great for consistent environments. Create a Dockerfile:

FROM python:3.10-slim
COPY script.py /script.py
CMD ["python", "/script.py"]

Build and run:

docker build -t my-python-app .
docker run my-python-app

Performance Tips

Python can be slow for some tasks. Here are tips to improve performance:

  • Use built-in functions instead of loops
  • Use list comprehensions
  • Profile your code with cProfile
  • Consider using PyPy for speed

Security Considerations

When running Python on Linux, keep security in mind:

  • Do not run scripts from untrusted sources
  • Use virtual environments to isolate dependencies
  • Keep Python and packages updated
  • Avoid running scripts as root

Frequently Asked Questions

How Do I Run A Python Script In Linux Terminal?

Navigate to the script’s directory and type python3 scriptname.py. Make sure Python is installed.

Can I Run Python 2 And Python 3 On The Same Linux System?

Yes, you can have both. Use python for Python 2 and python3 for Python 3. Some systems alias python to Python 3.

What Is The Difference Between Python And Python3 Commands?

python usually refers to Python 2 on older systems, while python3 explicitly calls Python 3. Modern distributions may alias python to Python 3.

How Do I Install Pip On Linux?

Use your package manager. For Ubuntu: sudo apt install python3-pip. For Fedora: sudo dnf install python3-pip.

Why Is My Python Script Not Running?

Check for syntax errors, missing modules, or permission issues. Ensure you are using the correct Python version and that the script is executable.

Now you have a complete guide on how to run python on linux. From installation to advanced topics like virtual environments and cron jobs, you are ready to start coding. Practice by writing simple scripts and gradually move to complex projects. The Linux terminal is your friend, and Python makes it even more powerful.

Remember to always verify your Python version and environment. Use virtual environments for project isolation. With these skills, you can automate tasks, build applications, and explore data science on Linux.

If you encounter any issues, refer back to this guide. The community is also helpful—forums like Stack Overflow have answers to most problems. Happy coding on your Linux machine!