pip安装python软件包,默认网站:https://pypi.python.org/simple/
目前国内常用源主要有:
阿里云:http://mirrors.aliyun.com/pypi/simple/
中国科技大学:https://pypi.mirrors.ustc.edu.cn/simple/
豆瓣:http://pypi.douban.com/simple/
清华大学:https://pypi.tuna.tsinghua.edu.cn/simple/
中国科学技术大学:http://pypi.mirrors.ustc.edu.cn/simple/Linux 及 Aid Learning 中更换国内源:
- pip3的配置文件为 pip.conf
文件可能位置:
/etc/pip.conf
~/.pip/pip.conf
~/.pip.conf
如果没有,则新建文件,已用: /etc/pip.conf, 下面依次为例。
- 修改文件内容:
cd /etc # 切换到需要修改的目录
vim pip.conf # 创建并打开 pip.conf, 也可以用 vi pip.conf, 系统默认会有 vi 编辑器。
编辑文件内容:
[global]
index-url = [http://mirrors.aliyun.com/pypi/simple/](http://mirrors.aliyun.com/pypi/simple/)
extra-index-url = [http://pypi.douban.com/simple/](http://pypi.douban.com/simple/)
[install]
trusted-host =
[mirrors.aliyun.com](http://mirrors.aliyun.com/)
[pypi.douban.com](http://pypi.douban.com/)
- 保存并退出文件编辑。
按 ESC 进入命令模式
输入 :wq 命令并回车。
- Windows 中配置文件路径:
C:\Users\你的用户名(zhangsan)\AppData\Roaming\pip\pip.ini
其中AppData可能被隐藏,需要打开查看隐藏文件夹
pip文件夹可能不存在,则需要创建 pip 文件夹及 pip.ini 文件
修改内容同上。
- 直接安装,直接 -i 加 url 即可! 如下:
pip install aiohttp -i http://pypi.douban.com/simple/
如果有报错:
使用命令:
pip install aiohttp -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com
- 临时安装 的 Python 脚本:
import os
package = input('Please input the package you want to install: ')
command = f'pip install {package} -i http://pypi.mirrors.ustc.edu.cn/simple --trusted-host pypi.mirrors.ustc.edu.cn'
os.system(command)
- Python脚本,修改配置文件:
import os
import platform
def main(os_type):
dir_user = os.path.expanduser('~')
if 'Linux' == os_type:
file1 = os.path.join(dir_user, '.pip', 'pip.conf')
file2 = os.path.join(dir_user, '.pip.conf')
if os.path.isfile(file1):
file_path = file1
elif os.path.isfile(file2):
file_path = file2
else:
file_path = '/etc/pip.conf'
elif 'Windows' == os_type:
file_path = os.path.join(dir_user, 'AppData', 'Roaming', 'pip', 'pip.ini')
dir_path = os.path.join(dir_user, 'AppData', 'Roaming', 'pip')
if not os.path.isdir(dir_path):
os.makedirs(dir_path)
else:
print("Doesn't support the system: ", os_type)
return
fo = open(file_path, 'a+')
fo.write(
'''
[global]
index-url = http://mirrors.aliyun.com/pypi/simple/
extra-index-url = http://pypi.douban.com/simple/
[install]
trusted-host =
mirrors.aliyun.com
pypi.douban.com
'''
)
fo.close()
print('Finish the configuration')
if __name__ == '__main__':
os_type = platform.system()
main(os_type)
参考资料:
Aidlearning中pip更换国内源方法:http://new.aidlearning.net/d/90
linux更换pip源:https://blog.csdn.net/qq_39852676/article/details/101591865