python UV的一些命令集合
2025年9月11日 - 由Bo 0 评论 3 阅读
按照我们这种上古时期开始写python的,现在习惯用pip安装包、用virtualenv配置虚拟环境、用pyenv管理python版本,前几年用conda。然后现在因为接触MCP,开始了解UV这个工具。
UV执行速度很快,而且功能相当于是集成了刚才列的几个工具的功能,可以生成uv.lock来固定环境,跟pyproject.toml也集成在一起。用惯了的话,还是很方便的。
这里就对一些常用的命令做个记录备份
# install uv to manage python and virtualenv
curl -LsSf https://astral.sh/uv/install.sh | sh
# global config path
~/.config/uv/uv.toml
# init a project named qamcp, will create folder and some files like pyproject.toml, .gitignore, readme, .python-version
uv init qamcp
# create virtualenv, if not give venv name, the default folder is .venv
uv venv
uv venv venv
# list installed packages, notice if use 'pip list' will list the default python environment's packages
uv pip list
# install or upgrade or uninstall one package by pip
uv pip install requests==2.26.0
uv pip upgrade requests
uv pip uninstall requests
# add package to pyproject.toml
uv add requests
# install from requirements.txt and add to pyproject.toml
uv add -r requirements.txt -c constraints.txt
uv pip install -r requirements.txt
# install and assign index source
uv pip install -i https://pypi.tuna.tsinghua.edu.cn/simple/
uv add --default-index https://pypi.tuna.tsinghua.edu.cn/simple requests
# install dependencies in pyproject.toml
uv sync # install to default venv: .venv
uv sync --active # install to current active venv
# list python versions and install one version
uv python list
uv python install 3.12.8
# set default python version
uv python default 3.12
# limit the python version
uv python pin 3.11
# list dependencies
uv tree
uv tree --package fastmcp
# export installed packages to requirements.txt
uv pip freeze > requirements.txt
# export productions packages to requirements.txt and exclude dev packages
uv pip freeze --production > requirements.txt
# install to dev packages
uv pip install --dev pytest
##uv.lock
#[[tool.uv.index]]
## Optional name for the index.
#name = "tsinghua"
#url = "https://pypi.tuna.tsinghua.edu.cn/simple"
#default = true
#
#[[tool.uv.index]]
#name = "aliyun"
#url = "https://mirrors.aliyun.com/pypi/simple/"
之后再来更新