Python pip命令用法说明

熟话说工欲善其事必先利其器,在接触Python后一会都会用到,但是会不会用就是那么一回事了,下面就罗列出pip一些实用命令

pip freeze
与pip list不同的是,会同时罗列出库版本号


(env-rag-project-framework) root@siner-virtual-machine:~# pip freeze
accelerate==1.13.0
aiofiles==25.1.0
aiohappyeyeballs==2.6.2
aiohttp==3.14.0
aiosignal==1.4.0
annotated-doc==0.0.4
annotated-types==0.7.0
antlr4-python3-runtime==4.9.3
anyio==4.13.0
asgiref==3.11.1
async-timeout==4.0.3
attrs==26.1.0
backoff==2.2.1
bcrypt==5.0.0
beautifulsoup4==4.14.3
boto3==1.35.68
botocore==1.35.99
build==1.5.0
cachetools==7.1.4
certifi==2026.5.20
cffi==2.0.0
chardet==7.4.3
charset-normalizer==3.4.7
chroma-hnswlib==0.7.3
chromadb==0.5.3
click==8.4.1
coloredlogs==15.0.1
contourpy==1.3.2
cryptography==48.0.0
cycler==0.12.1
dataclasses-json==0.6.7
Deprecated==1.3.1
distro==1.9.0
durationpy==0.10


pip index versions {包名}

# 查看可用版本



(env-rag-project-framework) root@siner-virtual-machine:~# pip index versions unstructured-inference  
WARNING: pip index is currently an experimental command. It may be removed/changed in a future release without prior warning.
unstructured-inference (1.2.0)
Available versions: 1.2.0, 1.1.7, 1.1.6, 1.1.4, 1.1.2, 1.1.1, 1.0.5, 1.0.2, 0.8.10, 0.8.9, 0.8.7, 0.8.6, 0.8.5, 0.8.4, 0.8.3, 0.8.2, 0.8.1, 0.8.0, 0.7.41, 0.7.40, 0.7.39, 0.7.37, 0.7.36, 0.7.35, 0.7.34, 0.7.33, 0.7.32, 0.7.31, 0.7.29, 0.7.28, 0.7.27, 0.7.26, 0.7.25, 0.7.24, 0.7.23, 0.7.21, 0.7.20, 0.7.19, 0.7.18, 0.7.17, 0.7.16, 0.7.15, 0.7.14, 0.7.13, 0.7.12, 0.7.11, 0.7.10, 0.7.9, 0.7.8, 0.7.7, 0.7.6, 0.7.5, 0.7.4, 0.7.3, 0.7.2, 0.7.1, 0.7.0, 0.6.6, 0.6.5, 0.6.4, 0.6.3, 0.6.1, 0.5.31, 0.5.28, 0.5.27, 0.5.25, 0.5.24, 0.5.23, 0.5.22, 0.5.21, 0.5.20, 0.5.19, 0.5.18, 0.5.17, 0.5.16, 0.5.15, 0.5.14, 0.5.13, 0.5.12, 0.5.11, 0.5.10, 0.5.9, 0.5.8, 0.5.7, 0.5.6, 0.5.5, 0.5.4, 0.5.3, 0.5.2, 0.5.1, 0.4.4, 0.4.2, 0.4.1, 0.4.0, 0.3.2, 0.3.1, 0.3.0, 0.2.11, 0.2.10, 0.2.7, 0.2.5, 0.2.4, 0.2.3, 0.2.2, 0.2.1, 0.2.0
  INSTALLED: 0.7.31
  LATEST:    1.2.0

[notice] A new release of pip is available: 23.0.1 -> 26.1.2
[notice] To update, run: pip install --upgrade pip



pip install "unstructured-inference<0.8" "numpy<=1.26.4"

# 在安装某个包时同时限制库版本号


pip install --upgrade pdfplumber "numpy<=1.26.4"
# 升级某个库并加依赖限制


附加一个shell脚本方便进行依赖问题排查、



#!/bin/bash
# 用法: ./check_deps.sh 

requirements_file="${1}"

if [ -z "$requirements_file" ]; then
    echo "❌ 用法: $0 "
    echo "   示例: $0 requirements_ubun.txt"
    exit 1
fi

if [ ! -f "$requirements_file" ]; then
    echo "❌ 文件不存在: $requirements_file"
    exit 1
fi

echo "====== 依赖包安装状态检查 ======"
echo ""

total=0
installed=0
missing=0
mismatch=0

while IFS= read -r line || [ -n "$line" ]; do
    line_trimmed=$(echo "$line" | sed 's/^[[:space:]]*//;s/[[:space:]]*$//')
    [ -z "$line_trimmed" ] && continue
    [[ "$line_trimmed" == \#* ]] && continue

    # 提取包名和版本约束字符串(保留 == 及之后部分)
    pkg_name=$(echo "$line_trimmed" | sed 's/[<>=!].*//' | sed 's/^[[:space:]]*//;s/[[:space:]]*$//')
    constraint=$(echo "$line_trimmed" | grep -oE '[<>=!]+.*' | head -1)
    [ -z "$pkg_name" ] && continue

    total=$((total + 1))

    # 从 pip freeze 获取已安装版本
    result=$(pip freeze 2>/dev/null | grep -i "^${pkg_name}==")
    installed_version=$(echo "$result" | sed 's/.*==//')

    if [ -z "$result" ]; then
        echo "❌ $pkg_name  → 未安装"
        missing=$((missing + 1))
        continue
    fi

    # 已安装
    if [ -z "$constraint" ]; then
        # 无版本约束
        echo "✅ $result"
        installed=$((installed + 1))
    else
        # 有版本约束
        case "$constraint" in
            ==*)
                required_version=$(echo "$constraint" | sed 's/^==//')
                if [ "$installed_version" != "$required_version" ]; then
                    echo "! $result (要求 $required_version)"
                    mismatch=$((mismatch + 1))
                else
                    echo "✅ $result (精确匹配)"
                    installed=$((installed + 1))
                fi
                ;;
            # 其他约束类型(>=, <=, ~=, != 等)暂时仅显示已安装版本,标记 ?
            *)
                echo "? $result (约束: $constraint)"
                installed=$((installed + 1))
                ;;
        esac
    fi

done < "$requirements_file"

echo ""
echo "====== 汇总 ======"
echo "总计: $total"
echo "完全匹配/无约束: $installed"
echo "版本不匹配: $mismatch"
echo "缺失: $missing"



Zblog
YourCompany, Mitchell Admin 2026年6月5日
分析这篇文章
标签
我们的博客
存档
JetBrains系IDE AI插件推荐