如何安裝python模組 pip install numpy -i http://pypi.douban.com/simple --trusted-host pypi.douban.com

rick_grace發表於2019-01-04

報錯資訊:

Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ConnectTimeoutError(<pip._vendor.urllib3.connection.VerifiedHTTPSConnection object at 0x0581C150>, 'Connection to pypi.org timed out. (connect timeout=15)')': /simple/numpy/

 

原因:請求超時,資料來源有問題。

預設pip是使用Python官方的源,但是由於國外官方源經常被牆,導致不可用,我們可以使用國內的python映象源,從而解決Python安裝不上庫的煩惱。

 

解決辦法:

1、修改源(臨時):
可以在使用pip的時候在後面加上-i引數,指定pip源。

eg:    

pip install scrapy -i https://pypi.tuna.tsinghua.edu.cn/simple

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

如果有如下報錯:

請使用命令:

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

 

其他一些國內的pip源,如下:

阿里雲 http://mirrors.aliyun.com/pypi/simple/

中國科技大學 https://pypi.mirrors.ustc.edu.cn/simple/

豆瓣(douban) http://pypi.douban.com/simple/

清華大學 https://pypi.tuna.tsinghua.edu.cn/simple/

中國科學技術大學 http://pypi.mirrors.ustc.edu.cn/simple/

#### 注意後面要有/simple目錄!!! ####

 

2、修改源方法(永久修改):
linux:
修改 ~/.pip/pip.conf (沒有就建立一個), 內容如下:

[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple

windows:
直接在user目錄中建立一個pip目錄,如:C:\Users\xx\pip,新建檔案pip.ini,

windows在%HOMEPATH%\pip\pip.ini

內容如下:

[global]
index-url = http://pypi.douban.com/simple
[install]
trusted-host=pypi.douban.com

 
這樣在使用pip來安裝時,會預設呼叫該映象。

 

臨時使用其他源安裝軟體包的python指令碼如下:

#!/usr/bin/python

import os

package = raw_input("Please input the package which you want to install!\n")

command = "pip install %s -i http://pypi.mirrors.ustc.edu.cn/simple --trusted-host pypi.mirrors.ustc.edu.cn" % package

os.system(command)

 

 

也可以使用讀入檔案進行安裝。

ok,僅以記錄一下,以便於後期查閱!

------日期:2018年11月1日 增加Python配置pip預設源指令碼,複製到pip_source.py,執行即可。

#!/usr/bin/python
# coding: utf-8

import platform
import os

os_type = platform.system()
if "Linux" == os_type:
    fileDirPath = "%s/.pip" % os.path.expanduser('~')
    filePath = "%s/pip.conf" % fileDirPath
    if not os.path.isdir(fileDirPath):
        os.mkdir(fileDirPath)
    fo = open(filePath, "w")
    fo.write(
        "[global]\nindex-url=https://pypi.tuna.tsinghua.edu.cn/simple/\n[install]\ntrusted-host=pypi.tuna.tsinghua.edu.cn\n")
    fo.close()
    print "Configuration is complete"
elif "Windows" == os_type:
    fileDirPath = "%s\\pip" % os.path.expanduser('~')
    filePath = "%s\\pip.ini" % fileDirPath
    if not os.path.isdir(fileDirPath):
        os.mkdir(fileDirPath)
    fo = open(filePath, "w")
    fo.write(
        "[global]\nindex-url=https://pypi.tuna.tsinghua.edu.cn/simple/\n[install]\ntrusted-host=pypi.tuna.tsinghua.edu.cn\n")
    fo.close()
    print "Configuration is complete"
else:
    exit("Your platform is unknow!")

相關文章