Python 开发环境

最佳实践 6 分钟阅读 更新于 2024-11-09

1. Overview

Windows Linux/WSL

2. Windows

使用 uv 管理python版本和虚拟环境

2.1. uv

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# Install uv
$env:UV_DOWNLOAD_URL = "https://mirrors.ustc.edu.cn/github-release/astral-sh/uv/LatestRelease/"
powershell -ExecutionPolicy ByPass -c "irm https://mirrors.ustc.edu.cn/github-release/astral-sh/uv/LatestRelease/uv-installer.ps1 | iex"

# Upgrading uv
uv self update

# Add shell autocompletion
Add-Content -Path $PROFILE -Value @'
# uv
(& uv generate-shell-completion powershell) | Out-String | Invoke-Expression
$env:UV_PYTHON_INSTALL_MIRROR = "https://mirrors.ustc.edu.cn/github-release/astral-sh/python-build-standalone/"
$env:UV_DOWNLOAD_URL = "https://mirrors.ustc.edu.cn/github-release/astral-sh/uv/LatestRelease/"

'@

2.2. python

1
2
3
4
5
6
7
8
9
10
11
12
# list all available Python versions
uv python list

# install a Python version
uv python install 3.12

# set uv pip install mirror
Add-Content -Path $env:APPDATA\uv\uv.toml -Value @'
[[index]]
url = "https://mirrors.aliyun.com/pypi/simple"
default = true
'@

3. Linux/WSL

使用 uv 管理python版本和虚拟环境

3.1. uv

设置 uv 国内源

1
2
3
4
5
cat > ~/.config/uv/uv.toml <<- 'EOM'
[[index]]
url = "https://mirrors.ustc.edu.cn/pypi/simple"
default = true
EOM

安装相关环境变量

bash

1
2
3
4
5
cat >> ~/.bashrc <<- 'EOM'
# uv
export UV_PYTHON_INSTALL_MIRROR=https://mirrors.ustc.edu.cn/github-release/astral-sh/python-build-standalone/
export UV_DOWNLOAD_URL=https://mirrors.ustc.edu.cn/github-release/astral-sh/uv/LatestRelease/
EOM

fish

$HOME/.config/fish/conf.d/uv.fish

1
2
set -x UV_PYTHON_INSTALL_MIRROR "https://mirrors.ustc.edu.cn/github-release/astral-sh/python-build-standalone/"
set -x UV_DOWNLOAD_URL "https://mirrors.ustc.edu.cn/github-release/astral-sh/uv/LatestRelease/"

安装

1
2
3
4
5
6
# in new shell
# Install uv
curl -sL https://mirrors.ustc.edu.cn/github-release/astral-sh/uv/LatestRelease/uv-installer.sh | sh

# Upgrading uv
uv self update

3.2. python

usage

1
2
3
4
5
# list all available Python versions
uv python list

# install a Python version
uv python install 3.12

如果这篇文章对您有帮助,欢迎打赏支持