Python修改pip install 指定安装包的路径和默认镜像源

pip 默认会将依赖包安装到 Python 安装目录的 site-packages

一、修改pip安装镜像源

永久添加pip安装源

复制代码
pip config set global.index-url --site https://pypi.tuna.tsinghua.edu.cn/simple

可见,配置信息被写入pip.ini文件中,而此pip.ini被存放在python安装路径下。

打开该配置文件,可见:

复制代码
[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple

显然,与配置参数中的 golbal.index-url 对应。

查看pip.ini文件的存储位置有

复制代码
pip -v config list

可见:即,除了"site"对应的目录,还有其他目录可能存放pip配置文件。

查看pip config 的配置方法

复制代码
pip config -help

可见,下面的 [< file-option >] 参数,即为 --global 、--user 、--site,对应上面不同的目录。而--user是默认位置。

复制代码
Usage:
  pip config [<file-option>] list
  pip config [<file-option>] [--editor <editor-path>] edit

  pip config [<file-option>] get name
  pip config [<file-option>] set name value
  pip config [<file-option>] unset name
  pip config [<file-option>] debug


Description:
  Manage local and global configuration.

  Subcommands:

  - list: List the active configuration (or from the file specified)
  - edit: Edit the configuration file in an editor
  - get: Get the value associated with name
  - set: Set the name=value
  - unset: Unset the value associated with name
  - debug: List the configuration files and values defined under them

  If none of --user, --global and --site are passed, a virtual
  environment configuration file is used if one is active and the file
  exists. Otherwise, all modifications happen on the to the user file by
  default.

Config Options:
  --editor <editor>           Editor to use to edit the file. Uses VISUAL or EDITOR environment variables if not provided.
  --global                    Use the system-wide configuration file only
  --user                      Use the user configuration file only
  --site                      Use the current environment configuration file only

General Options:
  -h, --help                  Show help.
  --isolated                  Run pip in an isolated mode, ignoring environment variables and user configuration.
  -v, --verbose               Give more output. Option is additive, and can be used up to 3 times.
  -V, --version               Show version and exit.
  -q, --quiet                 Give less output. Option is additive, and can be used up to 3 times (corresponding to WARNING, ERROR, and CRITICAL logging levels).
  --log <path>                Path to a verbose appending log.
  --no-input                  Disable prompting for input.
  --proxy <proxy>             Specify a proxy in the form [user:passwd@]proxy.server:port.
  --retries <retries>         Maximum number of retries each connection should attempt (default 5 times).
  --timeout <sec>             Set the socket timeout (default 15 seconds).
  --exists-action <action>    Default action when a path already exists: (s)witch, (i)gnore, (w)ipe, (b)ackup, (a)bort.
  --trusted-host <hostname>   Mark this host or host:port pair as trusted, even though it does not have valid or any HTTPS.
  --cert <path>               Path to alternate CA bundle.
  --client-cert <path>        Path to SSL client certificate, a single file containing the private key and the certificate in PEM format.
  --cache-dir <dir>           Store the cache data in <dir>.
  --no-cache-dir              Disable the cache.
  --disable-pip-version-check
                              Don't periodically check PyPI to determine whether a new version of pip is available for download. Implied with --no-index.
  --no-color                  Suppress colored output
  --no-python-version-warning
                              Silence deprecation warnings for upcoming unsupported Pythons.
  --use-feature <feature>     Enable new functionality, that may be backward incompatible.
  --use-deprecated <feature>  Enable deprecated functionality, that will be removed in the future.

删除配置信息

复制代码
pip config --user unset site.index-url
pip config --user globalsite.index-url

把别处添加的源删除

二、查看当前安装位置

打开命令提示符或 PowerShell 窗口,使用如下命令来查看当前 pip 的包安装位置

bash 复制代码
pip show pip

输出如下信息,Location 行显示了 pip 当前的包安装位置:

bash 复制代码
Name: pip
Version: 24.0
Summary: The PyPA recommended tool for installing Python packages.
Home-page:
Author:
Author-email: The pip developers <distutils-sig@python.org>
License: MIT
Location: C:\Users\用户名\AppData\Local\Programs\Python\Python311\Lib
Requires:
Required-by:

也可以使用 python -m site 查看 Python 的​​模块搜索路径系统​ ​和​​包安装位置​

bash 复制代码
python -m site

输出如下信息:

bash 复制代码
sys.path = [
    'C:\\Users\\用户名',                   # 当前工作目录
    'C:\\Python312\\python312.zip',        # Python 标准库(压缩包)
    'C:\\Python312\\DLLs',                 # 动态链接库目录
    'C:\\Python312\\lib',                  # 标准库目录
    'C:\\Python312',                       # Python 安装根目录
    'C:\\Python312\\lib\\site-packages',   # 系统级包安装目录
]
USER_BASE: 'C:\\Users\\用户名\\AppData\\Roaming\\Python' (exists)
USER_SITE: 'C:\\Users\\用户名\\AppData\\Roaming\\Python\\Python312\\site-packages' (exists)
ENABLE_USER_SITE: True

三、更改 pip 的默认包安装位置

方法 1:在安装 Python 时,使用自定义安装

在初次安装 Python 时,如果指定了安装盘符(例如E盘),那么 pip 的默认安装路径也会随之改变。pip 默认会将第三方包安装到 Python 安装目录下的 Lib\site-packages 文件夹中。

方法 2:使用 pip install 的 --target 或 --prefix 参数 (每次安装时指定)

使用 pip install 命令的 --target 或 --prefix 参数,可以指定包安装的位置(临时指定),例如,我们希望将 pip 包安装到 E 盘。

bash 复制代码
# 每次安装时指定目标路径
pip install 包名 --target E:\你的自定义路径\Python\Python312\site-packages
 
# 或者使用--prefix参数
pip install 包名 --prefix E:\你的自定义路径\Python\Python312

这将会将依赖包安装到指定的目录下,而不是默认位置,但是这个方法只在当前的命令下有效。

注: 使用虚拟环境 的项目建议优先使用 --target 或**--prefix** 参数,构建项目级隔离。

方法 3:使用 pip.ini 配置文件

在用户目录下(C:\Users\用户名\AppData)创建 pip 文件夹pip.ini 配置文件

bash 复制代码
# 打开命令提示符或 PowerShell
mkdir %APPDATA%\pip
notepad %APPDATA%\pip\pip.ini

编辑 pip.ini 文件内容,这将覆盖默认的安装设置,使 pip 将依赖包安装到指定位置。

bash 复制代码
# 将路径替换为你想要的实际路径
[global]
target = E:\你的自定义路径\Python\Python312\site-packages
index-url=http://mirrors.aliyun.com/pypi/simple/     #指定镜像源
 
[install]
install-option = --prefix=E:\你的自定义路径\Python\Python312
方法 4:通过环境变量设置

右键 "此电脑" → 属性 → 高级系统 → 环境变量 → 新建环境变量

# 设置 PIP_TARGET 环境变量

变量名:PIP_TARGET

变量值:E:\你的自定义路径\Python\Python312\site-packages

# 设置 PYTHONPATH 环境变量

变量名:PYTHONPATH

变量值:E:\你的自定义路径\Python\Python312\site-packages

相关环境变量的说明及其关系

| 变量名 | 作用范围 | 优先级 | 用途 |
| VIRTUAL_ENV | 虚拟环境 | 最高 | 项目级完全隔离 |
| PYTHONUSERBASE | 用户级安装 (- -user) | 中 | 无权限时的包安装 |
| PIP_TARGET | 全局 pip 安装 | 低 | 修改所有pip安装路径 |

PYTHONPATH 模块搜索路径 自定义 添加额外导入路径
方法 5:修改 site.py 文件

查看 site.py 存放路径,site.py 一般存放在 Python 安装目录下的 Lib 目录,也可以使用命令查询

bash 复制代码
python -c "import site; print(site.__file__)"

打开 site.py 文件,编辑以下内容,修改为你的自定义路径:

修改前:

修改后:

注: 如果设置了环境变量(无论值为何),Python 都会跳过用户级的 site-packages,即,如果环境变量的设置有效,就无需修改 site.py 文件。

验证设置

再次查看安转路径,尝试运行一个 Python 项目并使用 pip install 进一步验证。

注: 如果之前已经使用 pip install 将依赖包安装到 site-packages 目录下,可以在修改完安装目录后直接将之前的 site-packages 目录剪切到新的目录下,无需重新下载依赖。

引用:https://blog.csdn.net/Fr_Favour/article/details/152129146

相关推荐
沈浩(种子思维作者)6 小时前
真的能精准医疗吗?癌症能提前发现吗?
人工智能·python·网络安全·健康医疗·量子计算
C_心欲无痕6 小时前
ts - tsconfig.json配置讲解
linux·前端·ubuntu·typescript·json
njsgcs7 小时前
ue python二次开发启动教程+ 导入fbx到指定文件夹
开发语言·python·unreal engine·ue
io_T_T7 小时前
迭代器 iteration、iter 与 多线程 concurrent 交叉实践(详细)
python
冰西瓜6007 小时前
国科大2025操作系统高级教程期末回忆版
linux
华研前沿标杆游学7 小时前
2026年走进洛阳格力工厂参观游学
python
Carl_奕然7 小时前
【数据挖掘】数据挖掘必会技能之:A/B测试
人工智能·python·数据挖掘·数据分析
HIT_Weston8 小时前
93、【Ubuntu】【Hugo】搭建私人博客:面包屑(一)
linux·运维·ubuntu
AI小怪兽8 小时前
基于YOLOv13的汽车零件分割系统(Python源码+数据集+Pyside6界面)
开发语言·python·yolo·无人机
wszy18098 小时前
新文章标签:让用户一眼发现最新内容
java·python·harmonyos