11 篇文章带有标签 “pip”

彻底搞懂 uv pip、uv add 和 uv tool 的核心区别

在 Python 工具链大洗牌的今天,Astral 团队推出的 uv 已经成为了无可争议的“速度之王”。它不仅能用 Rust 带来百倍的速度提升,还展现出了统一 Python 生态的野心。

然而,很多刚从 pippoetry 迁移过来的开发者,在看到 uv pipuv adduv tool 这三个都在“装包”的命令时,难免会产生疑问:它们难道不是重合的吗?为什么装个包还要分三种命令?

我们就来彻底拆解这三者的设计哲学和应用场景,帮你建立起最清晰的 uv 工作流。

其实,这是 uv 为了彻底解决 Python 长期以来“全局环境污染”“虚拟环境混乱”以及“工具与项目依赖混淆”等痛点,而设计的三套完全独立的工作流。

命令 对应传统工具 管理的目标对象 核心作用
uv pip pip / pip-tools 底层虚拟环境中的包 作为原生 pip 的超快替代品,直接向当前激活的环境中塞入依赖。
uv add poetry add / pdm add 当前声明式项目的依赖 现代项目管理工作流。自动管理 pyproject.tomluv.lock
uv tool pipx 全局可执行工具(如 ruff, black 在完全隔离的专用环境中安装 CLI 工具,并自动暴露到全局,绝不污染项目。

虽然它们都是给项目安装依赖,但一个是命令式(Imperative),一个是声明式(Dec

加速 Docker 构建镜像

  • Debian
PRETTY_NAME="Debian GNU/Linux 11 (bullseye)"
NAME="Debian GNU/Linux"
VERSION_ID="11"
VERSION="11 (bullseye)"
VERSION_CODENAME=bullseye
ID=debian
HOME_URL="https://www.debian.org/"
SUPPORT_URL="https://www.debian.org/support"
BUG_REPORT_URL="https://bugs.debian.org/"

Ubuntu PRETTY_NAME="Ubuntu Jammy Jellyfish (development branch)" NAME="Ubuntu" VERSION_ID="22.04" VERSION="22.04 (Jammy Jellyfish)" VERSION_CODENAME=jammy ID=ubuntu ID_LIKE=debian HOME_URL="https://www.ubuntu.

Python 配置共享软件包缓存

共享软件包缓存的好处是,一旦用户已经下载了软件包的特定版本,它将不会再次下载并存储在单独的缓存中。这节省了磁盘使用量并加快了安装速度,因为它不需要再次下载软件包。

通过查看了解到我现在已经配置了共享软件包的缓存目录(package cache):/opt/miniconda/pkgs/Users/junjian/.conda/pkgs,根据顺序优先存放在 /opt/miniconda/pkgs 目录中。

配置完成后,可以通过运行 conda info 来查看配置是否生效。

Install Python3.9 in Ubuntu20.04

在看到这个错误,首先想到的就是安装包 python3.9-distutils,但是在安装的时候看到它依赖的安装包都是 python3.8 的。没有找到更好的解决方法,这里找到一个解释

I believe this is a bug in Debian's Python package. Their modifications to Python have been a source of a long standing debate: https://gist.github.com/tiran/2dec9e03c6f901814f6d1e8dad09528e has a lot of discussion.
I think that is the issue, Ubuntu packages are inconsistent, i.e. python3.8 and python3.9 bring different set of modules, as well as have different set of decencies, while all that should really differ is just Python version. Same applies for python3.[89]-minimal. However, all 4 are consentient in one thing - not having sys.prefix/lib/pythonX.Y/site-packages in sys.path
To have that (and distutils) sorted, one needs to install a collection of python3 packages, but those are Python3.8 (and bring tons of semi-random libraries and so).

All Ubuntu provided Python3.[89] 'installations' have ensurepip removed. I think, the 'logic' is to force people to use python3-pip that (unless --no-install-recommends is used) brings whole tone of things including make, cpp and perl(!)

I strongly recommend filing an issue with Debian and Ubuntu for this -- while pip can do things to paper over the issue, the fundamental problem is that the Python installation is not proper. Part of the problem is that Debian users don't ask Debian's maintainers to make fixes for the things they break.
I will consider that.

manually installing distutils
How did you do this? If you've used what is available in CPython's source tree, then you've installed an incompatible distutils for the Python interpreter -- Debian relies on patches they make to distutils to keep things working.
Installing might be a bit of overstatement, since I am working with Docker container I am doing:

COPY --from=python:3.9-slim /usr/local/lib/python3.9/distutils /usr/lib/python3.9/distutils
I would do FROM python:3.9-slim but I have to rely on specific 'base' container.
python:3.9-slim has FROM debian:bullseye-slim

Dockerfile 实践

  • DEBIAN_FRONTEND
  • 加速
    • apt 源
    • pypi 源
  • 安装
    • gcc
    • pip
    • Python
    • OpenCV

构建镜像

docker build -t gouchicao/ubuntu:20.04-python3-opencv4 .
  • requirements.txt 优先把 requirements.txt 文件拷贝到镜像中,然后安装依赖库,最后把程序代码拷贝进镜像中,主要是代码变动的频率比库变动的频率高,这样避免了每次都要重新安装依赖的库,加快构建镜像的速度。

基于PyPIServer创建私有Python软件包存储库

  • 客户端不仅可以下载还可以上传(当我们自己开发了Python的软件时)
#创建用户名和密码
sudo apt install apache2-utils -y
sudo mkdir /data/pypi-packages
sudo htpasswd -sc /data/pypi-packages/htpasswd.txt wjj
#当您需要再创建用户名时就不需要加参数 -c
sudo htpasswd -s /data/pypi-packages/htpasswd.txt test
#容器部署
docker run -d --restart=always --name pypiserver -p 8080:8080 \
    -v /data/pypi-packages/:/data/packages \
    pypiserver/pypiserver:latest -P /data/packages/htpasswd.txt

安装 pip3 install tensorflow Looking in indexes: http://172.16.33.174:8080/simple/, https://mirrors.aliyun.com/pypi/simple/ Collecting tensorflow Downloading http://172.16.33.