python

关注公众号 jb51net

关闭
首页 > 脚本专栏 > python > Python修改pip install指定路径和默认镜像源

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

作者:爱吃面条的猿

这篇文章主要介绍了如何修改Python的pip安装路径和默认镜像源,文章首先介绍了如何通过修改pip.ini文件、设置环境变量、修改site.py文件等方式来更改pip的安装位置,需要的朋友可以参考下

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 的包安装位置

pip show pip

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

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 的​​模块搜索路径系统​​和​​包安装位置​

python -m site

输出如下信息:

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 盘。

# 每次安装时指定目标路径
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 配置文件

# 打开命令提示符或 PowerShell
mkdir %APPDATA%\pip
notepad %APPDATA%\pip\pip.ini

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

# 将路径替换为你想要的实际路径
[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 目录,也可以使用命令查询

python -c "import site; print(site.__file__)"

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

修改前:

修改后:

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

验证设置

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

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

以上就是Python修改pip install指定安装包的路径和默认镜像源的操作指南的详细内容,更多关于Python修改pip install指定路径和默认镜像源的资料请关注脚本之家其它相关文章!

您可能感兴趣的文章:
阅读全文