python-env

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Python Environment Management

Python 环境管理

Virtual environments, dependency management, and Python version control.
虚拟环境、依赖管理与Python版本控制。

venv (Built-in)

venv(内置工具)

bash
undefined
bash
undefined

Create virtual environment

Create virtual environment

python -m venv .venv python3 -m venv .venv
python -m venv .venv python3 -m venv .venv

Activate

Activate

source .venv/bin/activate # Linux/macOS .venv\Scripts\activate # Windows
source .venv/bin/activate # Linux/macOS .venv\Scripts\activate # Windows

Deactivate

Deactivate

deactivate
deactivate

Install packages

Install packages

pip install requests flask pip install -r requirements.txt
pip install requests flask pip install -r requirements.txt

Freeze dependencies

Freeze dependencies

pip freeze > requirements.txt
pip freeze > requirements.txt

Upgrade pip

Upgrade pip

pip install --upgrade pip
undefined
pip install --upgrade pip
undefined

requirements.txt

requirements.txt

undefined
undefined

Pinned versions (production)

Pinned versions (production)

flask==3.0.0 requests==2.31.0 sqlalchemy==2.0.23
flask==3.0.0 requests==2.31.0 sqlalchemy==2.0.23

Ranges (library development)

Ranges (library development)

flask>=3.0,<4.0 requests~=2.31 # Compatible release (>=2.31, <3.0)
flask>=3.0,<4.0 requests~=2.31 # Compatible release (>=2.31, <3.0)

With extras

With extras

fastapi[all]==0.104.0
fastapi[all]==0.104.0

From git

From git

Separate dev dependencies

Separate dev dependencies

pip install -r requirements.txt pip install -r requirements-dev.txt
undefined
pip install -r requirements.txt pip install -r requirements-dev.txt
undefined

Poetry

Poetry

bash
undefined
bash
undefined

Install Poetry

Install Poetry

curl -sSL https://install.python-poetry.org | python3 -
curl -sSL https://install.python-poetry.org | python3 -

Create new project

Create new project

poetry new my-project poetry init # In existing project
poetry new my-project poetry init # In existing project

Add dependencies

Add dependencies

poetry add requests flask poetry add pytest --group dev poetry add "sqlalchemy>=2.0,<3.0"
poetry add requests flask poetry add pytest --group dev poetry add "sqlalchemy>=2.0,<3.0"

Remove dependency

Remove dependency

poetry remove requests
poetry remove requests

Install all dependencies

Install all dependencies

poetry install poetry install --without dev
poetry install poetry install --without dev

Update dependencies

Update dependencies

poetry update poetry update requests # Single package
poetry update poetry update requests # Single package

Show dependencies

Show dependencies

poetry show poetry show --tree
poetry show poetry show --tree

Run command in virtualenv

Run command in virtualenv

poetry run python main.py poetry run pytest
poetry run python main.py poetry run pytest

Activate shell

Activate shell

poetry shell
poetry shell

Build & publish

Build & publish

poetry build poetry publish
poetry build poetry publish

Export to requirements.txt

Export to requirements.txt

poetry export -f requirements.txt --output requirements.txt poetry export -f requirements.txt --without-hashes
undefined
poetry export -f requirements.txt --output requirements.txt poetry export -f requirements.txt --without-hashes
undefined

pyproject.toml (Poetry)

pyproject.toml (Poetry)

toml
[tool.poetry]
name = "my-project"
version = "1.0.0"
description = "My project"
authors = ["Name <email@example.com>"]
python = "^3.11"

[tool.poetry.dependencies]
python = "^3.11"
flask = "^3.0"
sqlalchemy = "^2.0"

[tool.poetry.group.dev.dependencies]
pytest = "^8.0"
ruff = "^0.1"
mypy = "^1.7"

[tool.poetry.scripts]
serve = "my_project.main:app"
toml
[tool.poetry]
name = "my-project"
version = "1.0.0"
description = "My project"
authors = ["Name <email@example.com>"]
python = "^3.11"

[tool.poetry.dependencies]
python = "^3.11"
flask = "^3.0"
sqlalchemy = "^2.0"

[tool.poetry.group.dev.dependencies]
pytest = "^8.0"
ruff = "^0.1"
mypy = "^1.7"

[tool.poetry.scripts]
serve = "my_project.main:app"

uv (Fast Python Package Manager)

uv(快速Python包管理器)

bash
undefined
bash
undefined

Install uv

Install uv

Create virtual environment

Create virtual environment

uv venv
uv venv

Install packages (10-100x faster than pip)

Install packages (10-100x faster than pip)

uv pip install requests flask uv pip install -r requirements.txt
uv pip install requests flask uv pip install -r requirements.txt

Sync from lock file

Sync from lock file

uv pip sync requirements.txt
uv pip sync requirements.txt

Compile requirements (resolve + lock)

Compile requirements (resolve + lock)

uv pip compile requirements.in -o requirements.txt
uv pip compile requirements.in -o requirements.txt

Create project

Create project

uv init my-project
uv init my-project

Add dependency

Add dependency

uv add requests uv add pytest --dev
uv add requests uv add pytest --dev

Run

Run

uv run python main.py
undefined
uv run python main.py
undefined

pyenv (Python Version Management)

pyenv(Python版本管理)

bash
undefined
bash
undefined

Install pyenv

Install pyenv

curl https://pyenv.run | bash
curl https://pyenv.run | bash

List available versions

List available versions

pyenv install --list | grep "3."
pyenv install --list | grep "3."

Install Python version

Install Python version

pyenv install 3.12.1 pyenv install 3.11.7
pyenv install 3.12.1 pyenv install 3.11.7

Set global default

Set global default

pyenv global 3.12.1
pyenv global 3.12.1

Set local version (per directory)

Set local version (per directory)

pyenv local 3.11.7 # Creates .python-version
pyenv local 3.11.7 # Creates .python-version

List installed

List installed

pyenv versions
pyenv versions

Uninstall

Uninstall

pyenv uninstall 3.10.0
undefined
pyenv uninstall 3.10.0
undefined

Pipenv

Pipenv

bash
undefined
bash
undefined

Install

Install

pip install pipenv
pip install pipenv

Create environment + install

Create environment + install

pipenv install pipenv install requests pipenv install pytest --dev
pipenv install pipenv install requests pipenv install pytest --dev

Run in environment

Run in environment

pipenv run python main.py pipenv shell
pipenv run python main.py pipenv shell

Lock dependencies

Lock dependencies

pipenv lock
pipenv lock

Install from lock file

Install from lock file

pipenv install --deploy
pipenv install --deploy

Show dependency graph

Show dependency graph

pipenv graph
undefined
pipenv graph
undefined

conda

conda

bash
undefined
bash
undefined

Create environment

Create environment

conda create -n myenv python=3.12 conda create -n myenv python=3.12 numpy pandas
conda create -n myenv python=3.12 conda create -n myenv python=3.12 numpy pandas

Activate/deactivate

Activate/deactivate

conda activate myenv conda deactivate
conda activate myenv conda deactivate

Install packages

Install packages

conda install numpy pandas conda install -c conda-forge package-name
conda install numpy pandas conda install -c conda-forge package-name

List environments

List environments

conda env list
conda env list

Export environment

Export environment

conda env export > environment.yml
conda env export > environment.yml

Create from file

Create from file

conda env create -f environment.yml
conda env create -f environment.yml

Remove environment

Remove environment

conda env remove -n myenv
undefined
conda env remove -n myenv
undefined

Common Issues

常见问题

bash
undefined
bash
undefined

"pip not found" after creating venv

"pip not found" after creating venv

python -m pip install --upgrade pip
python -m pip install --upgrade pip

Permission errors

Permission errors

pip install --user package-name
pip install --user package-name

Conflicting versions

Conflicting versions

pip install --force-reinstall package==1.0
pip install --force-reinstall package==1.0

Clear pip cache

Clear pip cache

pip cache purge
pip cache purge

Check what's outdated

Check what's outdated

pip list --outdated
pip list --outdated

Verify installation

Verify installation

python -c "import package; print(package.version)"
undefined
python -c "import package; print(package.version)"
undefined

Reference

参考资料

For comparison and migration guides:
references/tools.md
如需对比和迁移指南,请查看:
references/tools.md