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

相关推荐
程序员龙叔4 小时前
编写高质量 Skill 系列 -- 如何设计需求分析与用例生成的 SKILL
自动化测试·软件测试·python·软件测试工程师·接口测试·性能测试·skill·ai测试
用户8356290780516 小时前
使用 Python 操作 Word 内容控件
后端·python
摇滚侠6 小时前
Linux CentOS7 rpm 安装 MySQL 5.7
linux·运维·mysql
bush47 小时前
嵌入式linux学习记录十四、术语
linux·嵌入式
载数而行5207 小时前
Linux 11 动态监控指令top
linux
码云骑士8 小时前
32-慢查询排查全流程(下)-索引优化实战与最左前缀原则
python
不会C语言的男孩8 小时前
Linux 系统编程 · 第 8 章:进程基础
linux·c语言
古城小栈8 小时前
Unix 与 Linux 异同小叙
linux·服务器·unix
闵孚龙8 小时前
《PyTorch 深度修炼》Dataset 和 DataLoader:数据如何喂给模型
人工智能·pytorch·python