Python设置 -- 安装源修改

  1. 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/

  2. Linux 及 Aid Learning 中更换国内源:

  • pip3的配置文件为 pip.conf

文件可能位置:

  • /etc/pip.conf

  • ~/.pip/pip.conf

  • ~/.pip.conf

如果没有,则新建文件,已用: /etc/pip.conf, 下面依次为例。

  • 修改文件内容:
  1. cd /etc # 切换到需要修改的目录

  2. vim pip.conf # 创建并打开 pip.conf, 也可以用 vi pip.conf, 系统默认会有 vi 编辑器。

  3. 编辑文件内容:

[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/)
  1. 保存并退出文件编辑。
  • 按 ESC 进入命令模式

  • 输入 :wq 命令并回车。

  1. Windows 中配置文件路径:

C:\Users\你的用户名(zhangsan)\AppData\Roaming\pip\pip.ini

  • 其中AppData可能被隐藏,需要打开查看隐藏文件夹

  • pip文件夹可能不存在,则需要创建 pip 文件夹及 pip.ini 文件

  • 修改内容同上。

  1. 直接安装,直接 -i 加 url 即可! 如下:

pip install aiohttp -i http://pypi.douban.com/simple/

如果有报错:


image.png

使用命令:

pip install aiohttp -i http://pypi.douban.com/simple/ --trusted-host pypi.douban.com

  1. 临时安装 的 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)
  1. 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)

参考资料:

  1. Aidlearning中pip更换国内源方法:http://new.aidlearning.net/d/90

  2. linux更换pip源:https://blog.csdn.net/qq_39852676/article/details/101591865

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容