🔗 高优先级工具已拆分细粒度页:

  • uv
  • pip
  • pipx 收拢 pip、pipx、Poetry、uv、uvx、pip-tools、hatch、PDM、Rye 等依赖与项目管理工具。

导航

收录来源

  • raw/langs/pythons/tools/dependency.rst
  • raw/langs/pythons/tools/dependencys/pip.rst
  • raw/langs/pythons/tools/dependencys/pipx.rst
  • raw/langs/pythons/tools/dependencys/easyinstall.rst
  • raw/langs/pythons/tools/dependencys/Poetry.rst
  • raw/langs/pythons/tools/dependencys/uv.rst
  • raw/langs/pythons/tools/dependencys/uvx.rst
  • raw/langs/pythons/tools/dependencys/pip-tools.rst
  • raw/langs/pythons/tools/dependencys/hatch.rst
  • raw/langs/pythons/tools/dependencys/PDM.rst
  • raw/langs/pythons/tools/dependencys/Rye.md

条目内容

pip的使用

安装Pip

  • Pip 是安装python包的工具, 提供了安装包, 列出已经安装的包, 升级包以及卸载包的功能

python命令安装:

python -m ensurepip --default-pip

系统包管理器安装:

sudo apt install python-pip

sudo yum install python-pip

brew install pip
  • get_pip.py 脚本安装:

    $ curl -0 https://raw.github.com/pypa/pip/master/contrib/get-pip.py
    $ sudo python get-pip.py
    
  • 通过源文件来安装:

    $ wget http://pypi.python.org/packages/source/p/pip/<pip-0.7.2>.tar.gz  # 替换为最新的包
    $ tar xzf pip-0.7.2.tar.gz
    $ cd pip-0.7.2
    $ python setup.py install
    
  • 通过easy_install来安装:

    $ easy_install pip
    

pip install命令

安装最新版本的包:

$ pip install Markdown

安装特定版本的package:

通过使用==, >=, <=, >, <来指定一个版本号
$ pip install 'Markdown<2.0'
$ pip install 'Markdown>2.0,<2.0.3'
$ pip install applicationName==version

-U

升级已安装的包:

升级包到当前最新的版本,可以使用-U 或者 --upgrade
$ pip install -U Markdown

-i(—index-url)

指定安装源 -i(--index-url) :

pip install package_name -i <URL>
示例:
pip install requests -i https://mirrors.aliyun.com/pypi/simple/

-e(—editable)

安装开发版本(开发者模式) -e(--editable) :

# 以“开发模式”安装包。
# 在这种模式下,包的源代码文件会被链接到 Python 环境中,而不是复制。
# 这意味着对源代码的任何更改都会立即反映在已安装的包中。
pip install -e .
or
pip install --editable .

# 示例
pip install -e ".[torch,metrics]"
# 说明
# [torch,metrics]:这些是额外的依赖项名称
# 你的 setup.py 文件中可能有类似以下的定义
# extras_require={
#     'torch': ['torch>=1.8.0'],
#     'metrics': ['some-metrics-library']
# }

—no-deps

--no-deps 参数的作用:

在安装指定的 Python 包时忽略其依赖项
具体来说,--no-deps 参数会阻止 pip 自动安装该包所需的其他依赖包,仅安装用户指定的包本身

示例:
pip install torch --nodeps

使用场景:
    1. 避免依赖冲突(可能已经手动安装了特定版本的依赖包,而自动安装的依赖可能会导致版本冲突)
    2. 加快安装速度(如果依赖包不是必需的,忽略依赖可以加快安装过程)
    3. 开发和调试(开发环境中,如果你只需测试某个包的核心功能而不需要依赖项)

—no-cache-dir

直接指定 whl 文件安装:

pip install --no-cache-dir https://github.com/NetEase-FuXi/EETQ/releases/download/v1.0.0/EETQ-1.0.0+cu121+torch2.1.2-cp310-cp310-linux_x86_64.whl

—pre

  • 允许安装预发布版本(pre-release)的软件包
  • 预发布版本,用于测试新功能,通常包含 alpha、beta 或 rc(Release Candidate)等标识(如 1.1.0-beta.2)

使用github库安装

基本语法:

pip install git+https://github.com/huggingface/transformers.git[#subdirectory][extras]
示例:
pip install git+https://github.com/huggingface/transformers.git transformers[torch]
or
pip install "git+https://github.com/huggingface/transformers.git#egg=transformers[torch]"

指定分支、tag 或 commit:

# 主分支(默认)
pip install "git+https://github.com/huggingface/transformers.git@main#egg=transformers[torch]"

# 特定 tag(版本)
pip install "git+https://github.com/huggingface/transformers.git@v4.30.0#egg=transformers[torch]"

# 特定 commit
pip install "git+https://github.com/huggingface/transformers.git@abc1234567890#egg=transformers[torch]"

Installing from local archives

Install a particular source archive file:

pip install ./downloads/SomeProject-1.0.4.tar.gz

Install from a local directory containing archives (and don’t check PyPI):

python3 -m pip install --no-index --find-links=file:///local/dir/ SomeProject
python3 -m pip install --no-index --find-links=/local/dir/ SomeProject
python3 -m pip install --no-index --find-links=relative/dir/ SomeProject

Installing “Extras”

Extras 是软件包的可选 “变体”,其中可能包括额外的依赖项,从而启用软件包中的其他功能:

pip install 'SomePackage[PDF]'
pip install 'SomePackage[PDF]==3.0'
pip install -e '.[PDF]'  # editable project in current directory

指定额外的依赖项:

pip install package_name[extra]
示例: 安装 requests 以及其额外的 security 依赖项
pip install requests[security]

direct_url.json

下面这些命令执行时会生成 direct_url.json 文件:

pip install https://example.com/app-1.0.tgz
pip install https://example.com/app-1.0.whl
pip install "app @ git+https://example.com/repo/app.git#subdirectory=setup"
pip install ./app
pip install file:///home/user/app
pip install --editable "app @ git+https://example.com/repo/app.git#subdirectory=setup"
pip install -e ./app

下面这些命令执行时不会生成 direct_url.json 文件:

pip install app
pip install app --no-index --find-links https://example.com/

查询pip包

  • 列出安装的packages:

    $ pip freeze > requirements.txt
    or
    $ pip list
    
    # 查看过时的包:
    pip list --outdated
    
  • 查询包:

    $ pip search "Markdown"     ⛔️
    
    $ pip list | grep Markdown  ✅
    

Warning

PyPI 停止支持 pip search 命令的日期是在 2018 年 4 月。这个决定是由于 XML-RPC search 在性能和安全性方面存在问题,因此被弃用。在 PyPI 的更新和升级过程中,许多旧的或不安全的功能都被移除或替代。如果你想查找 Python 包,现在推荐使用 https://pypi.org/search 网站上的搜索功能

  • 查看包的详细信息(包括其安装路径):

    pip show package_name
    

其他

  • 卸载包:

    $ pip uninstall Markdown
    

清理缓存:

pip cache purge

查询路径

  • 增加查询路径:

    sys.path.insert(0, '/usr/local/lib')
    
    sys.path.append('/usr/local/lib')
    
  • 默认路径优先级:

    1. 当前工作目录(可以通过导入的包名直接使用)
    2. PYTHONPATH目录(环境变量指定的目录)
    3. 标准库目录(Python安装目录里的Lib)
    4. site-packages目录(第三方模块安装目录)
    5. 用户目录(主目录下的.local/lib/pythonX.X/site-packages)
    6. /usr/local/lib/pythonX.X/dist-packages
    7. /usr/lib/pythonX.X/dist-packages
    

环境变量

  • 包安装后的py文件路径: Linux/macOS:venv/lib/pythonX.X/site-packages

修改安装目录:

方法1:
pip install -t /content/notebooks numpy

方法2:
export PYTHONUSERBASE=/content/notebooks
pip install numpy

镜像源

https://pypi.douban.com/simple/ 豆瓣
https://mirrors.aliyun.com/pypi/simple/ 阿里
https://pypi.hustunique.com/simple/ 华中理工大学
https://pypi.sdutlinux.org/simple/ 山东理工大学
https://pypi.mirrors.ustc.edu.cn/simple/ 中国科学技术大学
https://pypi.tuna.tsinghua.edu.cn/simple 清华
http://mirrors.cloud.tencent.com/pypi/simple 腾讯

选项:

-i,--index-url <url>
      Base URL of Python Package Index (default https://pypi.python.org/simple/).

使用示例:

// -i 就是选择镜像源
pip install --upgrade jupyterhub -i https://pypi.douban.com/simple/

全部配置:

# 配置清华PyPI镜像(如无法运行,将pip版本升级到>=10.0.0)
$ pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
Writing to /Users/zhaoweiguo/.config/pip/pip.conf

# 重置
$ pip config unset global.index-url

使用不同镜像源示例:

// cuda 11.7
pip3 install torch torchvision torchaudio

// cuda 11.8
pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118

// cpu
pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cpu

常见问题

较老版本的pip 中括号[]在 pip 安装命令中有特殊含义:

$ pip install huggingface-hub[cli]
zsh: no matches found: huggingface-hub[cli]

正确的做法:
$ pip install 'huggingface-hub[cli]'

requirements.txt

生成requirements.txt的方法:

1. 适用于「单虚拟环境」
pip freeze > requirements.txt
说明: 会将环境中的依赖包全都加入,所以只适合「单虚拟环境」
2. 推荐用法: 使用pipreqs
pip install pipreqs
pipreqs . --encoding=utf8 --force

使用requirements.txt安装依赖的方法:

$ pip install -r requirements.txt

实战

### 安装/升级 pip
python -m pip install --trusted-host=mirrors.aliyun.com -i https://mirrors.aliyun.com/pypi/simple/ --upgrade pip
### 基于pip依次安装第三方包和aos-cube
pip install --trusted-host=mirrors.aliyun.com -i https://mirrors.aliyun.com/pypi/simple/   setuptools
pip install --trusted-host=mirrors.aliyun.com -i https://mirrors.aliyun.com/pypi/simple/   wheel
pip install --trusted-host=mirrors.aliyun.com -i https://mirrors.aliyun.com/pypi/simple/   aos-cube

对比

  • Pip 是对 easy_install 的取代, 提供了和easy_install相同的查找包的功能, 因此可以使用easy_install安装的包也同样可以使用pip进行安装

site-packages和dist-packages的区别:

site-packages目录更适合本地编译安装的Python库。
dist-packages目录更适合系统包管理工具(如apt、yum等)全局安装的库。

site-packages目录需要当前用户有写入权限。
dist-packages目录需要root/管理员权限才能写入。

导入模块时,Python会先搜索site-packages目录,然后是dist-packages目录。

一些Linux发行版默认使用dist-packages,如Ubuntu。
一些使用site-packages,如CentOS。

pipx

Note

一些Python写的命令行工具(如mypy, flake8, black, and Pipenv),期望在os的任何地方运行,但如果都安装到 global environment 会造成版本依赖冲突等问题。pipx solves this by creating a virtual environment for each package, while also ensuring that its applications are accessible through a directory that is on your $PATH.

简介

Note

pipx only works with Python 3.6+.

  • pipx is a tool to help you install and run end-user applications written in Python. It’s roughly similar to macOS’s brew, JavaScript’s npx, and Linux’s apt.
  • It’s closely related to pip. In fact, it uses pip, but is focused on installing and managing Python packages that can be run from the command line directly as applications.
  • [How is it Different from pip?] → pip is a general-purpose package installer for both libraries and apps with no environment isolation. pipx is made specifically for application installation, as it adds isolation yet still makes the apps available in your shell: pipx creates an isolated environment for each application and its associated packages.

Warning

It is not recommended to install pipx via pipx. If you’d like to do this anyway, take a look at the pipx-in-pipx project and read about the limitations there.

安装

brew install pipx

// Ubuntu 23.04 or above
sudo apt install pipx

// Ubuntu 22.04 or below
python3 -m pip install --user pipx

// windows
scoop install pipx

使用

https://img.zhaoweiguo.com/uPic/2024/01/pipx_demo.gif

代码:

>> pipx install pycowsay
  installed package pycowsay 2.0.3, Python 3.7.3
  These apps are now globally available
    - pycowsay
done! ✨ 🌟 ✨

>> pipx list
venvs are in /home/user/.local/share/pipx/venvs
apps are exposed on your $PATH at /home/user/.local/bin
   package pycowsay 2.0.3, Python 3.7.3
    - pycowsay

# Now you can run pycowsay from anywhere
>> pycowsay mooo
  ____
< mooo >
  ====
         \
          \
            ^__^
            (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||

命令 pipx run APP [ARGS...] :

// Notice that you don't need to execute any install commands to run the app.
> pipx run pycowsay moo

  ---
< moo >
  ---
   \   ^__^
    \  (oo)\_______
       (__)\       )\/\
           ||----w |
           ||     ||

示例

  • 参见 demo_python 中的 pipx 命令

easyinstall命令

选项:

--upgrade (-U)     force upgrade (searches PyPI for latest versions)
sudo easy_install -U Sphinx

easy_install要卸载或删除某些软件包时:

sudo easy_install -m xxxx.egg
easy_install -mxN PackageName

Warning

已在 2021 年 1 月 23 日发布的 setuptools 版本 52 中删除。


Poetry

简介

  • 依赖管理和构建工具,提供了一种替代 pip 和 setuptools 的方法
  • Poetry requires Python 3.8+. It is multi-platform and the goal is to make it work equally well on Linux, macOS and Windows.
  • Poetry 是一个专注于 Python 项目依赖管理的工具,它使用 pyproject.toml 文件来定义项目元数据和依赖项。
  • 相比Conda:conda 是一个通用的包管理和环境管理工具,不仅可以管理 Python 包,还可以管理其他语言的包;Poetry 更专注于 Python 项目的依赖和包管理,而 conda 则是一个通用的包管理器,适用于跨语言和跨平台的应用
  • Poetry 是一个现代化的 依赖和虚拟环境 管理工具,它不仅提供了 构建和打包 功能,还支持依赖管理、虚拟环境创建和发布。Poetry 使用 pyproject.toml 管理项目的元数据和依赖关系。与 Setuptools 类似,但提供了更高级的功能,如依赖版本的锁定和虚拟环境的自动管理。

安装:

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

Basic usage

https://img.zhaoweiguo.com/uPic/2024/01/peotry_demo.gif

Project setup

create our new project:

$ poetry new poetry-demo
=>
目录结构:
poetry-demo
├── pyproject.toml
├── README.md
├── poetry_demo
│   └── __init__.py
└── tests
    └── __init__.py

pyproject.toml:

[tool.poetry]
name = "poetry-demo"
version = "0.1.0"
description = ""
authors = ["Sébastien Eustace <sebastien@eustace.io>"]
readme = "README.md"
packages = [{include = "poetry_demo"}]

[tool.poetry.dependencies]
python = "^3.7"
...     # 增加各种依赖

[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"

Using your virtual environment

  • By default, Poetry creates a virtual environment in {cache-dir}/virtualenvs
Using poetry run

直接使用:

poetry run python your_script.py
Activating the virtual environment
  • The easiest way to activate the virtual environment is to create a nested shell with poetry shell.

  • 对应的退出命令:

    $ exit
    or
    $ deactivate
    

Installing dependencies

poetry install
Installing without poetry.lock
  • If you have never run the command before and there is also no poetry.lock file present, Poetry simply resolves all dependencies listed in your pyproject.toml file and downloads the latest version of their files.
  • When Poetry has finished installing, it writes all the packages and their exact versions that it downloaded to the poetry.lock file, locking the project to those specific versions. You should commit the poetry.lock file to your project repo so that all people working on the project are locked to the same versions of dependencies (more below).
Installing with poetry.lock
  • If there is already a poetry.lock file as well as a pyproject.toml file when you run poetry install, it means either you ran the install command before, or someone else on the project ran the install command and committed the poetry.lock file to the project
  • Running install when a poetry.lock file is present resolves and installs all dependencies that you listed in pyproject.toml, but Poetry uses the exact versions listed in poetry.lock to ensure that the package versions are consistent for everyone working on your project.

poetry config

poetry config virtualenvs.create false --local
# 配置项:virtualenvs.create 是 Poetry 中控制是否自动创建虚拟环境的开关
# 设置为 false 意味着:不自动创建虚拟环境,而是在当前环境(比如系统的 Python 环境或你已经激活的虚拟环境)中安装依赖
# --local: 表示:将配置写入当前项目的 .venv 配置文件中(通常是 poetry.toml)。也就是说,这个配置只对当前项目有效,不会影响其他项目或全局设置

poetry export

Note

poetry export 这个命令 并不是内置在 Poetry 本体中的,它是由官方维护的 插件 提供的。

安装 poetry-plugin-export:

poetry self add poetry-plugin-export

✅ 标准导出命令(不带 dev 依赖):

poetry export -f requirements.txt --without-hashes -o requirements.txt

✅ 导出开发依赖(比如测试工具):

poetry export -f requirements.txt --with dev --without-hashes -o requirements.txt

配置

cache-dir:

Environment Variable: POETRY_CACHE_DIR

// 默认目录
macOS: ~/Library/Caches/pypoetry
Windows: C:\Users\<username>\AppData\Local\pypoetry\Cache
Unix: ~/.cache/pypoetry

poe

Quick start

Install the Poe the Poet:

pipx install poethepoet

Define some tasks in your pyproject.toml:

[tool.poe.tasks]
test         = "pytest --cov=my_app"                         # a simple command task
serve.script = "my_app.service:run(debug=True)"              # python script based task
tunnel.shell = "ssh -N -L 0.0.0.0:8080:$PROD:8080 $PROD &"   # (posix) shell based task

Run your tasks via the CLI:

$ poe test -v tests/unit # extra CLI arguments are appended to the underlying command
# 等同于执行: pytest --cov=my_app -v tests/unit

uv命令

Note

🚀 A single tool to replace pip, pip-tools, pipx, poetry, pyenv, virtualenv, and more.

  • An extremely fast Python package and project manager, written in Rust.

常用操作

uv 替换 pip(Python 包管理):

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

uv 替换 pip-tools(依赖锁定 & 解析):

# 解析 pyproject.toml 并生成 requirements.txt
uv pip compile pyproject.toml --output-file requirements.txt

uv 替换 pipx(安装全局 CLI 工具):

# 全局安装 CLI 工具
uv venv install httpx
# 运行 black(无需全局安装)
uv venv run black --version

uv 替换 poetry(项目依赖管理):

# 初始化项目(生成新项目,里面有文件pyproject.toml)
uv init <proj>
# 读取 pyproject.toml 文件中的 [tool.uv.dependencies] 部分,自动安装列出的依赖库
uv pip install
# 依赖添加到你的 pyproject.toml 文件中并安装
uv add uvicorn
# 安装项目依赖(基于uv.lock),如果没有.venv会创建
uv sync
uv sync --index-url https://mirrors.aliyun.com/pypi/simple/

uv 替换 virtualenv(虚拟环境管理):

# 创建一个新的 Python 虚拟环境(类似 python -m venv)
# 说明:不指定<path>时,<path>=.venv
uv venv <path>

Warning

⚠️ uv 不能直接替代 pyenv,但可以配合 pyenv 使用。

Project management

  • uv manages project dependencies and environments, with support for lockfiles, workspaces, and more, similar to rye or poetry:

    $ uv init example
    Initialized project `example` at `/home/user/example`
    
    $ cd example
    
    $ uv add ruff
    Creating virtual environment at: .venv
    Resolved 2 packages in 170ms
       Built example @ file:///home/user/example
    Prepared 2 packages in 627ms
    Installed 2 packages in 1ms
     + example==0.1.0 (from file:///home/user/example)
     + ruff==0.5.4
    
    $ uv run ruff check
    All checks passed!
    

Tool management

  • uv executes and installs command-line tools provided by Python packages, similar to pipx.

  • Run a tool in an ephemeral environment using uvx (an alias for uv tool run):

    $ uvx pycowsay 'hello world!'
    Resolved 1 package in 167ms
    Installed 1 package in 9ms
     + pycowsay==0.0.0.2
      """
    
      ------------
    < hello world! >
      ------------
       \   ^__^
        \  (oo)\_______
           (__)\       )\/\
               ||----w |
               ||     ||
    

Install a tool with uv tool install:

$ uv tool install ruff
Resolved 1 package in 6ms
Installed 1 package in 2ms
 + ruff==0.5.4
Installed 1 executable: ruff

$ ruff --version
ruff 0.5.4

Python management

  • uv installs Python and allows quickly switching between versions.

  • Install multiple Python versions:

    $ uv python install 3.10 3.11 3.12
    Searching for Python versions matching: Python 3.10
    Searching for Python versions matching: Python 3.11
    Searching for Python versions matching: Python 3.12
    Installed 3 versions in 3.42s
     + cpython-3.10.14-macos-aarch64-none
     + cpython-3.11.9-macos-aarch64-none
     + cpython-3.12.4-macos-aarch64-none
    

Download Python versions as needed:

$ uv venv --python 3.12.0
Using Python 3.12.0
Creating virtual environment at: .venv
Activate with: source .venv/bin/activate

$ uv run --python pypy@3.8 -- python --version
Python 3.8.16 (a9dbdca6fc3286b0addd2240f11d97d8e8de187a, Dec 29 2022, 11:45:30)
[PyPy 7.3.11 with GCC Apple LLVM 13.1.6 (clang-1316.0.21.2.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>>>

Use a specific Python version in the current directory:

$ uv python pin pypy@3.11
Pinned `.python-version` to `pypy@3.11`

Script support

  • uv manages dependencies and environments for single-file scripts.

  • Create a new script and add inline metadata declaring its dependencies:

    $ echo 'import requests; print(requests.get("https://astral.sh"))' > example.py
    
    $ uv add --script example.py requests
    Updated `example.py`
    
    $ cat example.py
    # /// script
    # requires-python = ">=3.12"
    # dependencies = [
    #     "requests",
    # ]
    # ///
    import requests; print(requests.get("https://astral.sh"))
    

Then, run the script in an isolated virtual environment:

$ uv run example.py
Reading inline script metadata from: example.py
Installed 5 packages in 12ms
<Response [200]>

A pip-compatible interface

  • uv provides a drop-in replacement for common pip, pip-tools, and virtualenv commands.

Compile requirements into a platform-independent requirements file:

$ uv pip compile docs/requirements.in \
   --universal \
   --output-file docs/requirements.txt
Resolved 43 packages in 12ms

Create a virtual environment:

$ uv venv
Using Python 3.12.3
Creating virtual environment at: .venv
Activate with: source .venv/bin/activate

Install the locked requirements:

$ uv pip sync docs/requirements.txt
Resolved 43 packages in 11ms
Installed 43 packages in 208ms
 + babel==2.15.0
 + black==24.4.2
 + certifi==2024.7.4
 ...

⚠️ 注意事项

  • uv.lock 是为 uv + pyproject.toml 架构设计的,不含 channel 或平台信息,不适合直接映射到 conda。
  • conda 和 uv/pip 的依赖解析方式不同,存在不兼容风险,推荐用 uv 来管理 Python 包时,不要在同一环境里混装 Conda 包。
  • 如果你需要生成 .conda-lock 文件(支持跨平台构建),请参考 [conda-lock](https://github.com/conda/conda-lock) 工具,而不是 uv.lock。
+---------------------------------------------+---------------------------------------+--------+

目的 | 方法 | 推荐度 |

+===========================================+===================================+======+ | 想用 conda 创建环境 | 导出 requirements.txt 再用 conda | ⭐⭐⭐ | +---------------------------------------------+---------------------------------------+--------+ | 已有 conda 环境 | 在其中用 uv 安装 uv.lock 中的依赖 | ⭐⭐⭐⭐ | +---------------------------------------------+---------------------------------------+--------+ | 用 conda 管系统依赖,用 uv 管 Python 包 | 分层管理,最现代做法 | ⭐⭐⭐⭐⭐ | +---------------------------------------------+---------------------------------------+--------+


uvx

  • uvx 与 uv 的关系类似 pipx 与 pip 的关系

uvx command invokes a tool without installing it:

➜ uvx ruff
Installed 1 package in 5ms
Ruff: An extremely fast Python linter and code formatter.

Usage: ruff [OPTIONS] <COMMAND>
... ...

uvx pycowsay hello from uv:

Installed 1 package in 8ms

  -------------
< hello from uv >
  -------------
   \   ^__^
    \  (oo)\_______
       (__)\       )\/\
           ||----w |
           ||     ||

执行 http 命令:

$ uvx --from httpie http https://www.baidu.com
HTTP/1.1 200 OK
Accept-Ranges: bytes
Cache-Control: no-cache
Connection: keep-alive
Content-Length: 227
Content-Type: text/html
Date: Tue, 11 Mar 2025 11:45:37 GMT
P3p: CP=" OTI DSP COR IVA OUR IND COM ", CP=" OTI DSP COR IVA OUR IND COM "
Pragma: no-cache
Server: BWS/1.1
Set-Cookie: BD_NOT_HTTPS=1; path=/; Max-Age=300
Set-Cookie: BIDUPSID=AA25793A0DA2579A9625A048FB5D4DB9; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com
Set-Cookie: PSTM=1741693537; expires=Thu, 31-Dec-37 23:55:55 GMT; max-age=2147483647; path=/; domain=.baidu.com
Set-Cookie: BAIDUID=AA25793A0DA2579AD302CFCB61871C94:FG=1; max-age=31536000; expires=Wed, 11-Mar-26 11:45:37 GMT; domain=.baidu.com; path=/; version=1; comment=bd
Traceid: 1741693537041124890616202303862579725118
X-Ua-Compatible: IE=Edge,chrome=1
X-Xss-Protection: 1;mode=block

<html>
<head>
    <script>
        location.replace(location.href.replace("https://","http://"));
    </script>
</head>
<body>
    <noscript><meta http-equiv="refresh" content="0;url=http://www.baidu.com/"></noscript>
</body>
</html>

pip-tools

  • https://github.com/jazzband/pip-tools

  • pip-tools 从项目中直接使用的包列表中创建所有依赖项的锁定文件,并确保仅安装这些依赖项。

  • Python 包管理工具,旨在帮助开发者 更好地管理依赖包及其版本,确保环境的可重复性和可维护性。

  • 主要包含两个命令行工具:

    pip-compile
    pip-sync
    

pip-compile

  • 作用: 从一个简洁的 requirements.in 文件生成一个详细的、可直接使用的 requirements.txt 文件,锁定所有依赖的具体版本(包括子依赖)。

  • 优势:

    • 自动处理依赖冲突。
    • 可以轻松升级或固定某些依赖版本。
    • 类似于 poetry lock 或 npm package-lock.json 的功能,但更轻量灵活,适用于 pip 用户。
  • 示例:

    # 先写 requirements.in,里面只写你关心的包
    # requirements.in
    Django>=3.2
    requests
    
    # 然后运行
    pip-compile requirements.in
    
    # 生成 requirements.txt,内容大概是
    # Django==3.2.14
    # pytz==2024.1
    # requests==2.31.0
    # certifi==2024.2.2
    

pip-sync

  • 作用:根据 requirements.txt,同步你的虚拟环境,安装缺失依赖,删除多余依赖,确保环境和 requirements.txt 完全一致。

  • 示例:

    pip-sync requirements.txt
    

适合人群

  • 喜欢使用 pip,但又希望有类似于 poetry 或 pipenv 的依赖锁定能力。
  • 想要明确掌控依赖版本,避免 “依赖地狱”。
  • 团队协作中需要保证所有人开发环境一致。

hatch

  • https://github.com/pypa/hatch
  • Hatch 用于详细介绍项目管理工作流程中的更多步骤,例如递增版本和从项目模板创建新的骨架项目。

PDM


Rye简介

简介

  • Rye = Python 项目管理工具 + 虚拟环境管理器 + 构建发布工具,核心理念类似于:poetry + pyenv + pip-tools + build + twine 全部整合进一个统一工具中

安装

curl -sSf https://rye.astral.sh/get | bash

使用

常用命令:

rye init            # 初始化一个新项目(生成 pyproject.toml)
rye add requests    # 添加依赖
rye remove numpy    # 移除依赖
rye sync            # 安装所有依赖(自动创建虚拟环境)
rye run python      # 在虚拟环境中运行命令
rye build           # 构建项目(生成 wheel)
rye publish         # 发布到 PyPI

对比

功能/工具RyePoetryPip + venvConda
虚拟环境管理✅✅✅✅
项目初始化✅✅❌❌
构建 wheel✅✅❌❌
发布 PyPI✅✅❌❌
Python 下载✅(自动下载)❌❌✅
配置文件统一 pyproject.tomlpyproject.tomlrequirements.txtenvironment.yml