Configuration
python-Configuration Environment Configuration
- Virtual Environment Setup
bash
# Create virtual environment
python3.11 -m venv /opt/myapp/venv
# Activate virtual environment
source /opt/myapp/venv/bin/activate
# Upgrade pip and setuptools
pip install --upgrade pip setuptools wheel
- System-wide Configuration Site-packages Configuration:
python
# /usr/local/lib/python3.11/site-packages/sitecustomize.py
import sys
import os
# Add custom module paths
sys.path.insert(0, '/opt/company/python-modules')
# Set default encoding
import locale
locale.setlocale(locale.LC_ALL, 'en_US.UTF-8')
- Environment Variables
bash
# Essential Python environment variables
export PYTHONPATH="/opt/myapp/lib:/opt/shared/python"
export PYTHONDONTWRITEBYTECODE=1
export PYTHONUNBUFFERED=1
export PYTHONHASHSEED=random
export PIP_NO_CACHE_DIR=1
export PIP_DISABLE_PIP_VERSION_CHECK=1
- pip Configuration
ini
# ~/.pip/pip.conf or /etc/pip.conf
[global]
index-url = https://pypi.org/simple/
trusted-host = pypi.org
pypi.python.org
files.pythonhosted.org
timeout = 60
retries = 3
[install]
user = false
ignore-installed = true
no-deps = false
Security Configuration
- SSL/TLS Configuration
python
# Custom SSL context for pip and requests
import ssl
import certifi
# Use system certificates
ssl._create_default_https_context = ssl._create_unverified_context
- User and Permissions
bash
# Create dedicated Python user
sudo useradd -r -s /bin/false python-app
sudo mkdir -p /opt/myapp
sudo chown python-app:python-app /opt/myapp
sudo chmod 755 /opt/myapp