python 学习笔记
pip.ini 配置
C:\Users\admin\AppData\Roaming\pip\pip.ini
[global]
target = D:\Python\Python312\Lib\site-packages
index-url = https://pypi.tuna.tsinghua.edu.cn/simple
cache-dir = D:\python\pip-cache
uv
- uv cache dir
[System.Environment]::SetEnvironmentVariable('UV_CACHE_DIR', 'D:\uv\cache', 'User')
- uv python install dir
[System.Environment]::SetEnvironmentVariable('UV_PYTHON_INSTALL_DIR', 'D:\python', 'User')
python 列出依赖库
pip freeze > requirements.txt
uv pip freeze > requirements.txt
- 根据依赖文件卸载第三方库
pip uninstall -r requirements.txt -y
- 使用 uv
uv pip uninstall -r requirements.txt -y
创建和使用虚拟环境
## 创建虚拟环境
python -m venv venv
## 使用 uv
uv venv
## 激活虚拟环境
.\venv\Scripts\activate ## Windows
## 或者
source venv/bin/activate ## macOS 和 Linux
## 安装第三方库
pip install requests
## 使用 uv
uv pip install requests
## 卸载第三方库
pip uninstall requests
## 使用 uv
uv pip uninstall requests
## 生成依赖文件
pip freeze > requirements.txt
## 使用 uv
uv pip freeze > requirements.txt
## 根据依赖文件下载第三方库
pip install -r requirements.txt
## 使用 uv
uv pip install -r requirements.txt
## 根据依赖文件卸载第三方库
pip uninstall -r requirements.txt -y
## 使用 uv
uv pip uninstall -r requirements.txt -y
## 停用虚拟环境
deactivate
python pip 换源
清华源:
pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
查看源
pip config list
输出:
global.index-url='https://pypi.tuna.tsinghua.edu.cn/simple'
恢复默认源:
pip config unset global.index-url
修改 pip 安装的第三方库位置
在 C:\Users\admin\AppData\Roaming\pip\pip.ini 中写入
[global]
target = D:\Python\Python312\Lib\site-packages
查看第三方库的位置
python -m site
uv python -m site
查看指定库的详细信息
pip show requests
uv pip show requests
Name: requests
Version: 2.25.1
Summary: Python HTTP for Humans.
Home-page: https://requests.readthedocs.io
Author: Kenneth Reitz
Author-email: me@kennethreitz.org
License: Apache 2.0
Location: /path/to/your/env/lib/python3.8/site-packages
Requires: certifi, chardet, idna, urllib3
Required-by:
查看 Python 已安装的第三方库
pip list
uv pip list
| 功能 | uv 命令 |
pip 命令 |
永久修改方式 |
|---|---|---|---|
| 查找缓存 | uv cache dir |
pip cache dir |
- |
| 清理缓存 | uv cache clean |
pip cache purge |
- |
| 修改位置 | 环境变量 UV_CACHE_DIR |
环境变量 PIP_CACHE_DIR 或 配置文件 pip.ini |
uv cache
- 查看 uv cache 的位置
uv cache dir
- 删除 uv cache
uv cache clean
- 修改 uv cache dir
[System.Environment]::SetEnvironmentVariable('UV_CACHE_DIR', 'D:\uv\cache', 'User')
重启之后再运行 uv cache dir 就可以查看新的 uv cache 位置了
uv 管理多版本 python 的命令
[System.Environment]::SetEnvironmentVariable('UV_PYTHON_INSTALL_DIR', 'D:\python', 'User')
重启之后再运行 uv python dir 就可以查看新的 uv 下载多个版本 python 位置了
pip cache
- 查看 pip cache 的位置
pip cache dir
- 删除 pip cache
pip cache purge
- 修改 uv cache dir
在 pip.ini 中填入
[global]
cache-dir = D:\python\pip-cache
重启之后再运行 uv cache dir 就可以查看新的 uv cache 位置了
uv 换源
这里只谈项目级修改,不涉及全局修改
在 pyproject.toml 中加入
[tool.uv]
# 如果你只用这一个源,可以不写 index-strategy
index-strategy = "unsafe-best-match"
[[tool.uv.index]]
url = "https://pypi.tuna.tsinghua.edu.cn/simple/"
default = true
国内可用源
- 清华源
https://pypi.tuna.tsinghua.edu.cn/simple/
- 阿里云
https://mirrors.aliyun.com/pypi/simple/
- 腾讯云
https://mirrors.cloud.tencent.com/pypi/simple/
- 火山引擎
https://mirrors.volces.com/pypi/simple/
- 华为云
https://mirrors.huaweicloud.com/repository/pypi/simple/
- 中科大源
https://pypi.mirrors.ustc.edu.cn/simple/