最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python修改pip install指定安裝包的路徑和默認(rèn)鏡像源的操作指南

 更新時間:2025年12月03日 09:51:38   作者:愛吃面條的猿  
這篇文章主要介紹了如何修改Python的pip安裝路徑和默認(rèn)鏡像源,文章首先介紹了如何通過修改pip.ini文件、設(shè)置環(huán)境變量、修改site.py文件等方式來更改pip的安裝位置,需要的朋友可以參考下

pip 默認(rèn)會將依賴包安裝到 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

顯然,與配置參數(shù)中的 golbal.index-url 對應(yīng)。

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

pip -v config list

可見:即,除了“site”對應(yīng)的目錄,還有其他目錄可能存放pip配置文件。

查看pip config 的配置方法

pip config -help

可見,下面的 [< file-option >] 參數(shù),即為 --global 、–user 、–site,對應(yīng)上面不同的目錄。而–user是默認(rèn)位置。

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

把別處添加的源刪除

二、查看當(dāng)前安裝位置

打開命令提示符或 PowerShell 窗口,使用如下命令來查看當(dāng)前 pip 的包安裝位置

pip show pip

輸出如下信息,Location 行顯示了 pip 當(dāng)前的包安裝位置:

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 的??模塊搜索路徑系統(tǒng)??和??包安裝位置?

python -m site

輸出如下信息:

sys.path = [
    'C:\\Users\\用戶名',                   # 當(dāng)前工作目錄
    'C:\\Python312\\python312.zip',        # Python 標(biāo)準(zhǔn)庫(壓縮包)
    'C:\\Python312\\DLLs',                 # 動態(tài)鏈接庫目錄
    'C:\\Python312\\lib',                  # 標(biāo)準(zhǔn)庫目錄
    'C:\\Python312',                       # Python 安裝根目錄
    'C:\\Python312\\lib\\site-packages',   # 系統(tǒng)級包安裝目錄
]
USER_BASE: 'C:\\Users\\用戶名\\AppData\\Roaming\\Python' (exists)
USER_SITE: 'C:\\Users\\用戶名\\AppData\\Roaming\\Python\\Python312\\site-packages' (exists)
ENABLE_USER_SITE: True

三、更改 pip 的默認(rèn)包安裝位置

方法 1:在安裝 Python 時,使用自定義安裝

在初次安裝 Python 時,如果指定了安裝盤符(例如E盤),那么 pip 的默認(rèn)安裝路徑也會隨之改變。pip 默認(rèn)會將第三方包安裝到 Python 安裝目錄下的 Lib\site-packages  文件夾中。

方法 2:使用 pip install 的 --target 或 --prefix 參數(shù)(每次安裝時指定)

使用 pip install 命令的 --target 或 --prefix 參數(shù),可以指定包安裝的位置(臨時指定),例如,我們希望將 pip 包安裝到 E 盤。

# 每次安裝時指定目標(biāo)路徑
pip install 包名 --target E:\你的自定義路徑\Python\Python312\site-packages
 
# 或者使用--prefix參數(shù)
pip install 包名 --prefix E:\你的自定義路徑\Python\Python312

這將會將依賴包安裝到指定的目錄下,而不是默認(rèn)位置,但是這個方法只在當(dāng)前的命令下有效。

注:使用虛擬環(huán)境的項(xiàng)目建議優(yōu)先使用 --target  --prefix 參數(shù),構(gòu)建項(xiàng)目級隔離。

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

在用戶目錄下(C:\Users\用戶名\AppData)創(chuàng)建 pip 文件夾 和 pip.ini 配置文件

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

編輯 pip.ini 文件內(nèi)容,這將覆蓋默認(rèn)的安裝設(shè)置,使 pip 將依賴包安裝到指定位置。

# 將路徑替換為你想要的實(shí)際路徑
[global]
target = E:\你的自定義路徑\Python\Python312\site-packages
index-url=http://mirrors.aliyun.com/pypi/simple/     #指定鏡像源
 
[install]
install-option = --prefix=E:\你的自定義路徑\Python\Python312

方法 4:通過環(huán)境變量設(shè)置

右鍵 "此電腦" → 屬性 → 高級系統(tǒng) → 環(huán)境變量 → 新建環(huán)境變量

# 設(shè)置 PIP_TARGET 環(huán)境變量

變量名:PIP_TARGET 
變量值:E:\你的自定義路徑\Python\Python312\site-packages

# 設(shè)置 PYTHONPATH 環(huán)境變量

變量名:PYTHONPATH
變量值:E:\你的自定義路徑\Python\Python312\site-packages

相關(guān)環(huán)境變量的說明及其關(guān)系

變量名作用范圍優(yōu)先級用途
VIRTUAL_ENV虛擬環(huán)境最高項(xiàng)目級完全隔離
PYTHONUSERBASE用戶級安裝 (- -user)無權(quán)限時的包安裝
PIP_TARGET全局 pip 安裝修改所有pip安裝路徑
PYTHONPATH模塊搜索路徑自定義添加額外導(dǎo)入路徑

方法 5:修改 site.py 文件

查看 site.py 存放路徑,site.py 一般存放在 Python 安裝目錄下的 Lib 目錄,也可以使用命令查詢

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

打開 site.py 文件,編輯以下內(nèi)容,修改為你的自定義路徑:

修改前:

修改后:

注:如果設(shè)置了環(huán)境變量(無論值為何),Python 都會跳過用戶級的 site-packages,即,如果環(huán)境變量的設(shè)置有效,就無需修改 site.py 文件。

驗(yàn)證設(shè)置

再次查看安轉(zhuǎn)路徑,嘗試運(yùn)行一個 Python 項(xiàng)目并使用 pip install 進(jìn)一步驗(yàn)證。

注:如果之前已經(jīng)使用 pip install 將依賴包安裝到 site-packages 目錄下,可以在修改完安裝目錄后直接將之前的 site-packages 目錄剪切到新的目錄下,無需重新下載依賴。

以上就是Python修改pip install指定安裝包的路徑和默認(rèn)鏡像源的操作指南的詳細(xì)內(nèi)容,更多關(guān)于Python修改pip install指定路徑和默認(rèn)鏡像源的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評論

济宁市| 韩城市| 阿拉尔市| 石渠县| 新平| 南皮县| 永春县| 青铜峡市| 罗甸县| 普安县| 永春县| 星子县| 托里县| 枞阳县| 绥德县| 郓城县| 县级市| 张家港市| 遵化市| 定陶县| 高安市| 衡东县| 望谟县| 赫章县| 锡林浩特市| 河北区| 麻江县| 赤峰市| 门头沟区| 民丰县| 津市市| 定西市| 蓬莱市| 屯留县| 和顺县| 岳池县| 苗栗县| 年辖:市辖区| 蓬安县| 沅陵县| 阜新|