Python是一門開源、高級的、解釋性、通用性編程語言,可以應用於Web應用程序開發、人工智能、數據科學、網絡爬蟲等眾多領域。隨着Python的流行,越來越多的人開始學習Python,但是Python在安裝和使用過程中可能會面對國外鏡像源的下載速度過慢的問題,為了解決這個問題,我們需要更換Python使用的鏡像源。本篇文章將全面介紹Python更換鏡像源教程。
一、修改pip配置文件
pip是Python中的包管理工具,可以用來下載、安裝和升級Python模塊。我們可以通過修改pip的配置文件來更改默認的鏡像源,而不是手動每次都使用”-i”參數。
- 找到pip的配置文件
- 編輯pip配置文件
- 測試是否成功
[user_name@localhost ~]$ cd ~
[user_name@localhost ~]$ cd .pip
[user_name@localhost ~]$ ls
pip.conf
如果沒有pip.conf文件,我們可以新建一個
[user_name@localhost ~]$ mkdir .pip
[user_name@localhost ~]$ vi .pip/pip.conf
[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple/
這裡我們使用清華大學開源軟件鏡像站提供的鏡像源。之後保存並退出。
我們可以嘗試使用pip來下載一個Python模塊進行測試
[user_name@localhost ~]$ pip install requests
二、使用命令行參數
在使用pip安裝Python包時,我們可以通過”–index-url”參數來指定使用的鏡像源,這種方法比修改pip配置文件更直接。
- 使用清華大學的鏡像源
- 使用阿里雲的鏡像源
- 使用中國科學院開源軟件鏡像站
[user_name@localhost ~]$ pip install --index-url https://pypi.tuna.tsinghua.edu.cn/simple/ requests
[user_name@localhost ~]$ pip install --index-url http://mirrors.aliyun.com/pypi/simple/ requests
[user_name@localhost ~]$ pip install --index-url https://pypi.mirrors.ustc.edu.cn/simple/ requests
三、使用PyPI鏡像工具
如果你不想手動修改pip配置文件或者每次使用pip都需要輸入”–index-url”參數,那麼可以使用第三方工具來幫助你更換Python的鏡像源。
這裡我們介紹兩個流行的工具:pipenv和pipx。
- pipenv
- pipx
pipenv是一個Python依賴管理工具,可以創建虛擬環境、管理依賴、創建鎖文件等。同時,pipenv內置了使用國內鏡像源的功能,在默認情況下會使用清華大學的PyPI鏡像源。
安裝pipenv
[user_name@localhost ~]$ pip install pipenv
創建虛擬環境
[user_name@localhost ~]$ mkdir my_project
[user_name@localhost ~]$ cd my_project
[user_name@localhost my_project]$ pipenv --python 3.8
Creating a virtualenv for this project…
Pipfile: /home/user_name/my_project/Pipfile
Using /usr/bin/python3.8 (3.8.5) to create virtualenv…
⠰ Creating virtual environment...created virtual environment CPython3.8.5.final.0-64 in 101ms
creator CPython3Posix(dest=/home/user_name/.local/share/virtualenvs/my_project-gcY4sNNr, clear=True, global=False)
seeder FromAppData(download=False, pip=latest, setuptools=latest, wheel=latest, via=copy, app_data_dir=/home/user_name/.local/share/virtualenv)
added seed packages: pip==21.2.3, setuptools==57.4.0, wheel==0.37.0
activators BashActivator,FishActivator,PowerShellActivator,PythonActivator,XonshActivator
Virtualenv location: /home/user_name/.local/share/virtualenvs/my_project-gcY4sNNr
Creating a Pipfile for this project…
Installing django…
[env 1]/home/user_name/.local/share/virtualenvs/my_project-gcY4sNNr$ django
bash: django: command not found...
Install pexpect to enable auto-activation of virtual environments.
installing to ./.venv/...
✔ Success!
Virtualenv location: /home/user_name/my_project/.venv
Creating a Pipfile.lock...
Installing dependencies from Pipfile.lock (xxxxxx)...
========================================
Prepending 'PATH=/home/user_name/.local/share/virtualenvs/my_project-gcY4sNNr/bin:$PATH' to your shell.
To enable this command-line interface for other shells, install
[user_name@localhost my_project]$
會發現pipenv默認就會使用清華大學的PyPI鏡像源,無需額外配置。
pipx是一個在虛擬環境中安裝和管理Python應用程序包的工具。pipx使用了一組默認鏡像源,如果需要更改可以設置環境變量PIPX_DEFAULT_INDEX_URL來更換默認鏡像源。
安裝pipx
[user_name@localhost ~]$ python3 -m pip install --user pipx
[user_name@localhost ~]$ python3 -m pipx ensurepath
安裝Python應用程序包
[user_name@localhost ~]$ pipx install requests
之後pipx會使用默認鏡像源進行下載安裝。
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hk/n/184906.html