🐘

python

8 notes  •  PHP & Web Dev

Python Data Structures Quick Reference

A quick guide to Python's four built-in data structures and when to use each.

Choosing the Right Structure

  • list - ordered, mutable, duplicates allowed. Use for sequences that change.
  • tuple - ordered, immutable. Use for fixed records or dict keys.
  • dict - key-value pairs, fast lookup, mutable. Use for named access to data.
  • set - unordered, unique values only. Use to deduplicate or test membership.

Examples

# List
servers = ['web1', 'web2', 'db1']
servers.append('cache1')

# Tuple (immutable)
config = ('localhost', 3306)

# Dictionary
user = {'name': 'ravi', 'role': 'admin'}
print(user['role'])

# Set (unique values)
unique_ips = {'10.0.0.1', '10.0.0.2', '10.0.0.1'}
print(unique_ips)  # {'10.0.0.1', '10.0.0.2'}

Install and Use pip (Python Package Installer)

Install pip and use it to manage Python packages.

Install pip

# Ubuntu / Debian
sudo apt install python3-pip

# RHEL / CentOS
sudo yum install python3-pip

Common Commands

# Install a package
pip3 install requests

# Install a specific version
pip3 install django==4.2

# Upgrade pip itself
pip3 install --upgrade pip

# Uninstall
pip3 uninstall requests

# List installed packages
pip3 list

# Install from requirements file
pip3 install -r requirements.txt

# Export current environment
pip3 freeze > requirements.txt

Verify

pip3 --version

Fix "No module named apt_pkg" After Installing Python 3.x

After installing a newer Python version and setting it as default, apt tools may break with ModuleNotFoundError: No module named apt_pkg. The apt_pkg module is compiled for a specific Python version.

Fix - Copy the Compiled Module

cd /usr/lib/python3/dist-packages
ls -la | grep apt_pkg

Copy the versioned .so file to the generic name (adjust filename as needed):

sudo cp apt_pkg.cpython-36m-x86_64-linux-gnu.so apt_pkg.so

If you get "Too many levels of symbolic links":

sudo unlink apt_pkg.so
sudo cp apt_pkg.cpython-36m-x86_64-linux-gnu.so apt_pkg.so

Alternative - Fix with update-alternatives

sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.6 1
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.7 2
sudo update-alternatives --config python3

Select the Python version that matches the compiled apt_pkg (typically 3.6).

Verify

sudo apt update

Set Up Multiple Python Versions with Miniconda

Install multiple Python versions on Ubuntu using update-alternatives and Miniconda for isolated project environments.

Step 1 - Install Multiple Python Versions

sudo apt install software-properties-common
sudo apt-get install python3.7 python3.6

Step 2 - Register with update-alternatives

sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.6 1
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.7 2
sudo update-alternatives --config python3

Step 3 - Install Miniconda

wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh
bash Miniconda3-latest-Linux-x86_64.sh
# Restart shell after installation

Step 4 - Create a Conda Environment

conda create --name myproject python=3.7.5
conda activate myproject
python --version

Fix apt_pkg After Python Swap

cd /usr/lib/python3/dist-packages
sudo cp apt_pkg.cpython-36m-x86_64-linux-gnu.so apt_pkg.so

Django Management Commands Reference

Common django-admin and manage.py commands.

Project Setup

django-admin startproject myproject
python manage.py startapp myapp

Development Server

python manage.py runserver
python manage.py runserver 0.0.0.0:8080

Database

python manage.py makemigrations
python manage.py migrate
python manage.py loaddata fixture.json

Admin and Users

python manage.py createsuperuser
python manage.py changepassword username

Static Files and Shell

python manage.py collectstatic
python manage.py shell

Install Python 3.9 on Ubuntu 22.04

Ubuntu 22.04 ships with Python 3.10. Install Python 3.9 from the deadsnakes PPA.

Steps

sudo apt install software-properties-common
sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt update
sudo apt install python3.9 python3.9-distutils
sudo apt install python3-pip -y
pip3 --version

Set as Default (use update-alternatives - safer than ln -sf)

sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.10 1
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.9 2
sudo update-alternatives --config python3

Verify

python3.9 --version
python3 --version

Notes

  • Avoid using ln -sf to replace system python3 - it can break apt.

Fix "Failed building wheel for uWSGI" in Python

This error occurs due to an outdated pip, missing build tools, or missing Python development headers.

Step 1 - Install Build Dependencies

# Ubuntu / Debian
sudo apt-get install gcc python3-dev build-essential

# RHEL / CentOS
sudo yum install gcc python3-devel

# Alpine Linux
apk add gcc libc-dev

Step 2 - Upgrade pip and setuptools

pip install --upgrade pip wheel setuptools

Step 3 - Reinstall uWSGI

pip install uWSGI
# or a specific version:
pip install uWSGI==2.0.21

Verify

uwsgi --version

Troubleshooting

  • Try the pre-built wheel alternative: pip install pyuwsgi.
  • Use python3 -m pip install uWSGI to ensure correct pip.

Python Strings and Regex Quick Reference

Common Python string operations and regular expression patterns.

String Operations

s = "  Hello, World!  "

s.strip()                     # "Hello, World!"
s.lower()                     # "  hello, world!  "
s.replace("World", "Python")  # "  Hello, Python!  "
s.split(", ")                # ["  Hello", "World!  "]
",".join(["a","b"])          # "a,b"
s.find("World")              # 9

Regular Expressions

import re

text = "Server IP: 192.168.1.100, Port: 8080"

# Search for IP address
m = re.search(r"[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+", text)
if m: print(m.group())  # 192.168.1.100

# Find all numbers
nums = re.findall(r"[0-9]+", text)

# Replace whitespace
clean = re.sub(r"\s+", " ", "too   many   spaces")

# Case-insensitive search
re.search(r"server", text, re.IGNORECASE)