How to Install and Use Python on Debian
Python is one of the most popular programming languages in the world. It’s used for web development, data science, machine learning, automation, and more. This guide covers installing and using Python on Debian Linux.
Check Current Python Version
Most Debian versions come with Python pre-installed. Check what’s available:
python3 --version
python --version
which python3
which python
Method 1: Install Python from Debian Repository
Step 1: Update Package Lists
sudo apt update
Step 2: Install Python 3
sudo apt install -y python3
Step 3: Install pip (Package Manager)
pip is Python’s package manager:
sudo apt install -y python3-pip
Step 4: Install Additional Tools
Install common development tools:
sudo apt install -y python3-venv python3-dev build-essential
Method 2: Install Using pyenv (Recommended for Developers)
pyenv allows you to install and manage multiple Python versions:
Step 1: Install Dependencies
sudo apt install -y build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev curl libncursesw5-dev xz-utils tk-dev libxml2-dev libffi-dev liblzma-dev
Step 2: Install pyenv
curl https://pyenv.run | bash
Step 3: Configure pyenv
Add to your ~/.bashrc:
export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init --path)"
Step 4: Install Python Versions
pyenv install 3.12.0
pyenv install 3.11.5
pyenv global 3.12.0
Using Python
Run Python Interpreter
Start interactive Python session:
python3
Example session:
>>> print("Hello, World!")
Hello, World!
>>> 2 + 2
4
>>> exit()
Run a Python Script
Create and run a script:
nano hello.py
#!/usr/bin/env python3
print("Hello, World!")
for i in range(5):
print(i)
python3 hello.py
Using pip – Package Manager
Install a Package
pip3 install requests
Install Specific Version
pip3 install requests==2.31.0
List Installed Packages
pip3 list
Check for Updates
pip3 list --outdated
Upgrade a Package
pip3 install --upgrade requests
Uninstall a Package
pip3 uninstall requests
Show Package Info
pip3 show requests
Virtual Environments
Virtual environments keep project dependencies separate:
Step 1: Create Virtual Environment
python3 -m venv myenv
Step 2: Activate Virtual Environment
source myenv/bin/activate
Your prompt will change to show the environment name:
(myenv) user@server:~$
Step 3: Install Packages
Now install packages only in this environment:
pip install requests
Step 4: Deactivate
deactivate
Common Python Packages
- requests – HTTP library for web requests
- flask – Lightweight web framework
- django – Full-stack web framework
- numpy – Numerical computing
- pandas – Data analysis
- matplotlib – Data visualization
- scikit-learn – Machine learning
- beautifulsoup4 – Web scraping
- pillow – Image processing
- sqlalchemy – Database ORM
Example Projects
Web Scraper
pip3 install requests beautifulsoup4
import requests
from bs4 import BeautifulSoup
url = "https://example.com"
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
print(soup.title.text)
Simple HTTP Server
python3 -m http.server 8000
Access at: http://localhost:8000
Troubleshooting
pip: command not found
Use pip3 instead:
python3 -m pip install requests
Permission Denied Error
Don’t use sudo with pip. Use –user flag:
pip3 install --user requests
Old Python 2 Available
Python 2 is deprecated. Install Python 3:
sudo apt install python3 python3-pip
Conclusion
You now have Python installed and ready to use on Debian! Key commands to remember:
- python3 – Start Python interpreter
- pip3 install package – Install packages
- python3 -m venv – Create virtual environment
- pip3 list – List installed packages