Python pip 安装路径:查看与修改指南
在日常 Python 开发中,我们经常使用 pip 安装各种第三方库。了解这些包的安装位置对于管理 C 盘空间及多版本环境至关重要。本文将详细介绍如何查看和修改 pip 的默认安装路径。
一、如何查看 pip 安装路径
1. 使用 pip show 命令查看单个包路径
要查看特定包的安装位置,可以使用 pip show 命令:
pip show numpy
命令输出中的 Location 字段会显示该包的安装路径:
Name: numpy Version: 1.24.0 Summary: NumPy is the fundamental package for array computing with Python. Home-page: https://www.numpy.org/ Author: Travis E. Oliphant et al. Author-email: None License: BSD-3-Clause Location: E:\openSource\Python\Python311\Lib\site-packages Requires: Required-by:
2. 查看所有包路径的方法
如果想查看所有已安装包的位置,可以使用以下命令:
pip list -v
或者使用脚本批量查看:
# Windows
for /f "tokens=1" %i in ('pip list --format=freeze') do pip show %i | findstr "Location"
# Linux/Mac
pip list --format=freeze | awk -F '==' '{print $1}' | xargs -n1 pip show | grep Location
3. 使用 Python 命令查看基础路径
通过 Python 的 site 模块可以查看 Python 解释器查找包的所有目录:
python -m site
这会输出类似以下结果:
sys.path = [
'C:\\Users\\Administrator',
'E:\\openSource\\Python\\Python311\\python311.zip',
'E:\\openSource\\Python\\Python311\\DLLs',
'E:\\openSource\\Python\\Python311\\Lib',
'E:\\openSource\\Python\\Python311',
'E:\\openSource\\Python\\Python311\\Lib\\site-packages',
]
USER_BASE: 'C:\\Users\\Administrator\\AppData\\Roaming\\Python' (exists)
USER_SITE: 'C:\\Users\\Administrator\\AppData\\Roaming\\Python\\Python311\\site-packages' (exists)


