概述

历史

1989年圣诞节:Guido von Rossum开始写Python语言的编译器。1991年2月:第一个Python编译器(同时也是解释器)诞生,它是用C语言实现的(后面),可以调用C语言的库函数。在最早的版本中,Python已经提供了对“类”,“函数”,“异常处理”等构造块的支持,还有对列表、字典等核心数据类型,同时支持以模块为基础来构造应用程序。1994年1月:Python 1.0正式发布。2000年10月16日:Python 2.0发布,增加了完整的垃圾回收,提供了对Unicode的支持。与此同时,Python的整个开发过程更加透明,社区对开发进度的影响逐渐扩大,生态圈开始慢慢形成。 Python 3.7.x的版本是在2018年发布的,Python的版本号分为三段,形如A.B.C。其中A表示大版本号,一般当整体重写,或出现不向后兼容的改变时,增加A;B表示功能更新,出现新功能时增加B;C表示小的改动(例如:修复了某个Bug),只要有修改就增加C。

目前已经3.11.x的版本了, 具体> 3.7的内容后期介绍.

Understanding Python Environments

What is a Python Environment?

Think of a Python environment as a self-contained workspace for your Python projects. Just like you might have different toolboxes for different types of work (one for electrical work, another for plumbing), Python environments let you maintain separate sets of Python packages and dependencies for different projects.

environment == context

[!info] Why Do We Need Environments?

Imagine you're working on two different projects:

  • Project A needs Django version 3.2
  • Project B needs Django version 4.1

Without environments, you'd have a conflict. Python environments solve this by creating isolated spaces where each project can have its own version of packages without interfering with others.

The Global Python Environment

When you first install Python, you get what's called the "global" or "system" Python environment. This contains:

  • The Python interpreter itself
  • The standard library (built-in modules like os, sys, math)
  • Any packages you install globally using pip install

The problem with using only the global environment is that it becomes cluttered over time and can lead to dependency conflicts between projects.


Types of Python Environments

1. Virtual Environments (venv)

Virtual environments are lightweight, isolated Python environments. They're like creating a separate room in your house for each project.

Creating a Virtual Environment

# Create a new virtual environment
python -m venv myproject_env

# On macOS/Linux with specific Python version
python3.9 -m venv myproject_env

Activating and Deactivating

# Activate on macOS/Linux
source myproject_env/bin/activate

# Deactivate (works on all platforms)
deactivate

When activated, your command prompt typically shows the environment name, like (myproject_env) $.

2. Conda Environments

Conda is a more powerful environment manager that can handle not just Python packages, but also system libraries and packages from other languages.

# Create a new conda environment
conda create -n myproject python=3.9

# Activate conda environment
conda activate myproject

# Deactivate
conda deactivate

# List all environments
conda env list

3. Poetry Environments

Poetry is a modern dependency management tool that automatically creates and manages virtual environments.

# Initialize a new project with Poetry
poetry init

# Install dependencies and create environment
poetry install

# Run commands in the environment
poetry run python script.py

# Activate shell in environment
poetry shell

4. Pipenv Environments

Pipenv combines pip and virtualenv functionality with additional features.

# Create environment and install packages
pipenv install requests

# Activate environment
pipenv shell

# Run commands in environment
pipenv run python script.py

Virtual Environments Deep Dive

How Virtual Environments Work

When you create a virtual environment, Python creates a directory structure that contains:

myproject_env/
├── bin/ (or Scripts/ on Windows)
│   ├── activate
│   ├── pip
│   └── python
├── include/
├── lib/
│   └── python3.9/
│       └── site-packages/
└── pyvenv.cfg

The key insight is that activating an environment modifies your system's PATH variable to point to the environment's Python interpreter and pip, rather than the global ones.

Environment Variables and Activation

When you activate an environment, several things happen:

  1. ​PATH modification​: The environment's bin directory is added to the beginning of PATH
  2. ​VIRTUAL_ENV variable​: Set to the environment's directory path
  3. ​Prompt modification​: Your shell prompt is updated to show the active environment

Working with Requirements Files

Requirements files let you specify exactly which packages your project needs:

# requirements.txt
requests==2.28.1
flask>=2.0.0,<3.0.0
numpy>=1.21.0
pandas~=1.4.0

Version specifiers explained:

1. `==2.28.1`: Exact version
2. `>=2.0.0,<3.0.0`: Range (at least 2.0.0, less than 3.0.0)
3. `~=1.4.0`: Compatible release (equivalent to >=1.4.0, <1.5.0)
# Install from requirements file
pip install -r requirements.txt

# Generate requirements file from current environment
pip freeze > requirements.txt

Package Management

Understanding pip

Pip is Python's package installer. It downloads packages from the Python Package Index (PyPI) and installs them in your current environment.

# Install a package
pip install package_name

# Install specific version
pip install package_name==1.2.3

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

# Upgrade a package
pip install --upgrade package_name

# Uninstall a package
pip uninstall package_name

# List installed packages
pip list

# Show package information
pip show package_name

Understanding Package Dependencies

When you install a package, pip automatically installs its dependencies. For example, installing Django also installs its dependencies like asgiref, pytz, and sqlparse.

Development vs Production Dependencies

Many projects distinguish between:

  • ​Production dependencies​: Required for the application to run
  • ​Development dependencies​: Only needed during development (testing tools, linters, etc.)
# Poetry approach
poetry add requests  # Production dependency
poetry add --group dev pytest  # Development dependency

# Pip approach with separate files
pip install -r requirements.txt  # Production
pip install -r requirements-dev.txt  # Development

Python Module System

What Are Modules?

A module is simply a Python file containing Python code. It can define functions, classes, and variables, and can also include runnable code.

Types of Modules

1. Built-in Modules

These come with Python installation:

import os
import sys
import math
import datetime

2. Standard Library Modules

Part of Python's standard library but need to be imported:

import json
import urllib.request
import sqlite3
import threading

3. Third-party Modules

Installed via pip:

import requests
import pandas
import flask

4. Local Modules

Files you create in your project:

# mymodule.py
def greet(name):
    return f"Hello, {name}!"

# main.py
import mymodule
print(mymodule.greet("World"))

Module Search Path

Python looks for modules in a specific order:

  1. ​Current directory​: Where your script is located
  2. ​PYTHONPATH​: Environment variable with additional directories
  3. ​Standard library​: Python's built-in modules
  4. ​Site-packages​: Where pip installs packages

You can see the search path:

import sys
print(sys.path)

Import Statements

Basic Import

import math
result = math.sqrt(16)

Import with Alias

import numpy as np
array = np.array([1, 2, 3])

Import Specific Items

from math import sqrt, pi
result = sqrt(16)
print(pi)

Import All (not recommended)

from math import *
# This imports all public names from math

Packages vs Modules

A package is a collection of modules organized in directories. A package directory must contain an __init__.py file (which can be empty).

mypackage/
├── __init__.py
├── module1.py
├── module2.py
└── subpackage/
    ├── __init__.py
    └── module3.py
# Importing from packages
import mypackage.module1
from mypackage import module2
from mypackage.subpackage import module3

Creating Your Own Packages

Let's create a simple package structure:

# mathutils/__init__.py
from .basic import add, subtract
from .advanced import power, factorial

# mathutils/basic.py
def add(a, b):
    """Add two numbers."""
    return a + b

def subtract(a, b):
    """Subtract b from a."""
    return a - b

# mathutils/advanced.py
def power(base, exponent):
    """Calculate base raised to exponent."""
    return base  exponent

def factorial(n):
    """Calculate factorial of n."""
    if n <= 1:
        return 1
    return n * factorial(n - 1)

Now you can use it:

from mathutils import add, power
result = add(5, 3)
squared = power(4, 2)

Advanced Environment Management

Environment Management with pyenv

Pyenv allows you to easily switch between multiple versions of Python:

# Install Python version
pyenv install 3.9.7

# Set global Python version
pyenv global 3.9.7

# Set local Python version for current directory
pyenv local 3.8.10

# List available versions
pyenv versions

Managing Multiple Environments

For complex projects, you might need different environments for different purposes:

# Development environment
python -m venv dev_env
source dev_env/bin/activate
pip install -r requirements-dev.txt

# Testing environment
python -m venv test_env
source test_env/bin/activate
pip install -r requirements-test.txt

# Production environment
python -m venv prod_env
source prod_env/bin/activate
pip install -r requirements.txt

Environment Variables in Python

Environment variables are key-value pairs that exist in your operating system environment:

import os

# Get environment variable
database_url = os.getenv('DATABASE_URL', 'sqlite:///default.db')

# Set environment variable (only for current process)
os.environ['MY_VAR'] = 'some_value'

# Check if environment variable exists
if 'DEBUG' in os.environ:
    print("Debug mode enabled")

Using .env Files

For managing environment variables in development:

# .env file
DEBUG=True
DATABASE_URL=postgresql://user:pass@localhost/mydb
SECRET_KEY=your-secret-key-here
# Using python-dotenv
from dotenv import load_dotenv
import os

load_dotenv()  # Load variables from .env file

debug = os.getenv('DEBUG') == 'True'
database_url = os.getenv('DATABASE_URL')

Docker and Python Environments

Docker provides another level of environment isolation:

# dockerfile
FROM python:3.9-slim

WORKDIR /app

COPY requirements.txt .
RUN pip install -r requirements.txt

COPY . .

CMD ["python", "app.py"]

Best Practices and Troubleshooting

Best Practices

1. Always Use Virtual Environments

Never install packages globally unless absolutely necessary. Each project should have its own environment.

2. Pin Your Dependencies

Always specify exact versions in production:

# Good for production
django==4.1.2
requests==2.28.1

# Good for development
django>=4.1.0,<5.0.0
requests>=2.28.0

3. Separate Development and Production Dependencies

# requirements.txt (production)
django==4.1.2
psycopg2-binary==2.9.3

# requirements-dev.txt (development)
-r requirements.txt
pytest==7.1.2
black==22.8.0
flake8==5.0.4

4. Use Environment Variables for Configuration

import os

# Good
DATABASE_URL = os.getenv('DATABASE_URL')
DEBUG = os.getenv('DEBUG', 'False').lower() == 'true'

# Bad (hardcoded values)
DATABASE_URL = 'postgresql://user:pass@localhost/mydb'
DEBUG = True

5. Document Your Environment Setup

Create a README.md with setup instructions:

## Setup

1. Create virtual environment: `python -m venv venv`
2. Activate environment: `source venv/bin/activate` (Unix) or `venv\Scripts\activate` (Windows)
3. Install dependencies: `pip install -r requirements.txt`
4. Set up environment variables: Copy `.env.example` to `.env` and fill in values
5. Run the application: `python app.py`

Common Issues and Solutions

Problem: "Module not found" error

​Solution​:

  • Ensure you're in the correct virtual environment
  • Check if the package is installed: pip list
  • Verify the module name and import statement

Problem: Package conflicts

​Solution​:

  • Use virtual environments to isolate projects
  • Check for conflicting versions: pip show package_name
  • Consider using pip-tools or Poetry for better dependency resolution

Problem: Environment activation not working

​Solution​:

  • On Windows, you might need to run: Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser
  • Ensure you're using the correct activation script path
  • Try recreating the environment if it's corrupted

Problem: Packages installed in wrong environment

​Solution​:

  • Always check which Python you're using: which python or python --version
  • Verify pip is installing to the right location: pip --version
  • Use python -m pip instead of just pip to ensure you're using the right Python's pip

Problem: Import errors with local modules

​Solution​:

  • Ensure your module is in the Python path
  • Use relative imports within packages: from .module import function
  • Consider making your code a proper package with setup.py

Performance Considerations

Choosing the Right Environment Tool

  • ​venv​: Lightweight, comes with Python, good for simple projects
  • ​conda​: Better for data science, handles non-Python dependencies
  • ​Poetry​: Modern, great dependency resolution, good for libraries
  • ​pipenv​: Combines pip and virtualenv, good for applications

Managing Large Environments

For projects with many dependencies:

# Create environment with system site packages access
python -m venv myenv --system-site-packages

# Use conda for faster installations of scientific packages
conda install numpy pandas scipy

# Use pip-tools for better dependency management
pip-compile requirements.in
pip-sync requirements.txt

Debugging Environment Issues

Checking Your Python Configuration

import sys
import os

print("Python executable:", sys.executable)
print("Python version:", sys.version)
print("Python path:", sys.path)
print("Current working directory:", os.getcwd())
print("Environment variables:", dict(os.environ))

Verifying Package Installation

import pkg_resources

# List all installed packages
for package in pkg_resources.working_set:
    print(f"{package.project_name} == {package.version}")

This comprehensive guide should give you a solid foundation for understanding and working with Python environments and modules. Remember that mastering these concepts takes practice, so don't hesitate to experiment with different approaches to find what works best for your specific use cases.