Python包管理器整理笔记

1.pip

1.1 配置清华源

  • (1) 临时生效
1
>> pip install 包名 -i https://pypi.tuna.tsinghua.edu.cn/simple
  • (2) 永久生效
1
2
3
4
5
6
7
8
9
10
11
12
# 1.直接改配置
>> mkdir -p ~/.pip
>> echo "[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple
trusted-host = pypi.tuna.tsinghua.edu.cn
timeout = 60" > ~/.pip/pip.conf
# 2.命令行配置
>> pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
>> pip config set global.trusted-host pypi.tuna.tsinghua.edu.cn
# 3.验证配置
>> pip config list
>> pip config get global.index-url

2.conda

2.1 编辑配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# 1.生成配置文件
>> conda config --set show_channel_urls yes
# 2.编辑~/.condarc
>> vim ~/.condarc
## 清空原本内容,添加如下内容:
channels:
- defaults
show_channel_urls: true
default_channels:
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/msys2
custom_channels:
conda-forge: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
pytorch: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
menpo: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
bioconda: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
msys2: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud

2.2 命令行逐条添加

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# 1.添加默认主仓库
>> conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
>> conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r
>> conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/msys2
# 2.添加第三方云仓库
>> conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge
>> conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch
>> conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/bioconda
# 3.显示下载URL
>> conda config --set show_channel_urls yes
# 4.清空缓存
>> conda clean -i
>> conda clean --all # 全部缓存
# 5.验证配置
>> conda config --show channels
>> conda info
# 6.回复官方源
>> conda config --remove-key channels
>> conda config --set show_channel_urls no

3.uv

  • 安装:
1
2
3
4
# 1.curl命令
>> curl -LsSf https://astral.sh/uv/install.sh | sh
# 2.wget命令
>> wget -qO- https://astral.sh/uv/install.sh | sh

3.1 修改配置文件

1
2
3
4
5
6
7
8
# 1.创建配置文件存放路径
>> mkdir -p ~/.config/uv
# 2.添加配置
>> cat > ~/.config/uv/uv.toml << 'EOF'
[[index]]
url = "https://pypi.tuna.tsinghua.edu.cn/simple"
default = true
EOF

3.2 环境变量

1
2
3
>> vim ~/.zshrc
export UV_DEFAULT_INDEX="https://pypi.tuna.tsinghua.edu.cn/simple"
>> source ~/.zshrc

3.3 项目级配置

1
2
3
4
# 在pyproject.toml中添加:
[[tool.uv.index]]
url = "https://pypi.tuna.tsinghua.edu.cn/simple"
default = true

3.4 验证配置

1
2
3
4
# 1.查看uv当前配置
>> uv config list
# 2.测试安装
>> uv add numpy

3.5 配置多个备用源

  • (1) 编辑配置文件
1
2
3
4
5
6
7
# 编辑~/.config/uv/uv.toml
[[index]]
url = "https://pypi.tuna.tsinghua.edu.cn/simple"
default = true

[[index]]
url = "https://mirrors.aliyun.com/pypi/simple"
  • (2) 编辑环境变量
1
2
3
>> vim ~/.zshrc
export UV_INDEX="https://mirrors.aliyun.com/pypi/simple"
>> source ~/.zshrc

3.6 恢复官方源

1
2
>> rm ~/.config/uv/uv.toml
>> unset UV_DEFAULT_INDEX

4.uv包管理器用法

4.1 创建虚拟环境

1
2
3
4
5
6
7
8
# 1.当前目录生成 .venv 环境(默认python版本跟随系统)
>> uv venv

# 2.指定python版本
>> uv venv --python=3.10

# 3.指定自定义环境目录
>> uv env <ENV_PATH>

4.2 使用虚拟环境

  • (1) 激活环境使用
1
2
3
4
5
6
# 1.激活环境
>> source .venv/bin/activate
# 2.退出环境
>> deactivate
# 3.执行代码
>> python demo.py
  • (2) 不激活环境使用
1
>> uv run python demo.py

4.3 启动脚本

1
>> CUDA_VISIBLE_DEVICES=7 nohup uv run python main.py --listen 0.0.0.0 --port 8666 > comfyui1.log 2>&1 &