python - install with pip already satisfied build isolation
访问量: 1
refer to: deepseek
Question:
```
Collecting orjson~=3.0 (from gradio>=5.0.0->qwen-agent==0.0.16)
Using cached orjson-3.10.16.tar.gz (5.4 MB)
Running command pip subprocess to install build dependencies
Using pip 25.0.1 from /usr/local/lib/python3.10/site-packages/pip (python 3.10)
Collecting maturin<2,>=1
Using cached maturin-1.8.3.tar.gz (199 kB)
Installing build dependencies: started
Running command pip subprocess to install build dependencies
Using pip 25.0.1 from /usr/local/lib/python3.10/site-packages/pip (python 3.10)
Collecting setuptools
Obtaining dependency information for setuptools from https://files.pythonhosted.org/packages/54/21/f43f0a1fa8b06b32812e0975981f4677d28e0f3271601dc88ac5a5b83220/setuptools-78.1.0-py3-none-any.whl.metadata
# pip list | grep matu
maturin 1.8.3
```
I already installed maturin 1.8.3, why pip still need to install it? ( see the log above)
Anser by deepseek:
这个问题的根源在于 Python 包安装机制的工作方式。以下是详细解释和解决方案:
原因分析
构建隔离(Build Isolation)
Pip 默认会为每个包的构建过程创建独立的临时环境(即使已全局安装),这是为了:
避免构建依赖污染主环境
确保构建过程的可重复性
临时环境特性
日志中显示的 Running command pip subprocess to install build dependencies 表明:
Pip 正在为 orjson 的构建创建临时环境
该环境不会继承主环境已安装的包
Maturin 的双重角色
您全局安装的 maturin 1.8.3 是作为工具使用
orjson 将其列为构建依赖(build dependency),需要在构建时重新安装
解决方案
1. 禁用构建隔离(推荐)
bash
Copy
pip install orjson~=3.0 --no-build-isolation
✅ 优点:直接复用已安装的 maturin
⚠️ 注意:需确保全局环境的构建依赖完全匹配
2. 指定构建依赖版本
bash
Copy
pip install orjson~=3.0 --build-option="--build-requirement maturin==1.8.3"
3. 使用预编译轮子(最佳方案)
bash
Copy
pip install orjson --only-binary=orjson
完全跳过构建过程,直接下载二进制包
根本原因图示
mermaid
Copy
graph TD
A[主环境] -->|已安装| B[maturin 1.8.3]
C[orjson构建临时环境] -->|隔离| D[空白环境]
D -->|必须安装| E[maturin 1.8.3]
验证方法
查看实际使用的构建环境:
bash
Copy
pip debug --verbose | grep "Build isolation"
检查构建日志中的临时路径:
bash
Copy
cat $(find /tmp -name "pip-build-env-*" | head -1)/install.log
永久配置
在 ~/.pip/pip.conf 中添加:
ini
Copy
[install]
no-build-isolation = true
性能对比
方法 构建时间 安全性 适用场景
默认(隔离) 慢 高 生产环境
--no-build-isolation 快 中 开发环境
--only-binary 最快 高 有预编译包时
建议在龙芯架构上优先尝试 --only-binary,若失败再使用 --no-build-isolation。