关于python:Pip安装问题

Issue with Pip installation

两个问题:

首先是我尝试使用python调用JSON API,我在代码中使用了"导入请求"。 但得到一个错误说明:没有名为'requests'的模块

其次,当我尝试在cmd上安装pip时,我收到一条错误,指出 - 无法获取URL https://pypi.org/simple/pip:

我的代码可能有误。 请帮助解决这个问题。

命令提示符出错:

C:\Users\MEIPE\Desktop>python get-pip.py
Collecting pip
Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1056)'))': /simple/pip/ Retrying (Retry(total=3, connect=None, read=None, redirect=None, status=None))
after connection broken by 'SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1056)'))': /simple/pip/ Retrying (Retry(total=2, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1056)'))': /simple/pip/ Retrying (Retry(total=1, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1056)'))': /simple/pip/ Retrying (Retry(total=0, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1056)'))': /simple/pip/ Could not fetch URL https://pypi.org/simple/pip/: There was a problem confirming the ssl certificate: HTTPSConnectionPool(host='pypi.org', port=443): Max retries exceeded with url: /simple/pip/ (Caused by SSLError(SSLCertVerificationError
(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1056)'))) - skipping Could not find a version that satisfies the requirement pip (from versions: )No matching distribution found for pip Could not fetch URL https://pypi.org/simple/pip/: There was a problem confirming the ssl certificate: HTTPSConnectionPool(host='pypi.org', port=443): Max retries exceeded with url: /simple/pip/ (Caused by SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1056)'))) - skipping

码:

1
2
3
4
5
6
7
8
9
10
11
import urllib.parse
import requests

main_api = 'http://nagiosdatagateway.vestas.net/esq/ITE1452552/logstash-?'

date = '2018.12.16'
url = main_api + urllib.parse.urlencode({'date':date})

json_date = requests.get(url).json()

print(json_data)

想要调用动态更改的JSON API URL并将数据存储在SQL Server表中。 这将帮助我安排每天执行python脚本以获取JSON数据,然后从SQL表中,我将获得月度报告。


没有名为'requests'的模块:

这只是意味着你没有安装包"请求",它通过安装pip并运行命令解决(就像你可能发现的,从另一个问题判断):

1
pip install requests

PIP问题:

您从pip获得的错误表明SSL证书不正确。

如果您位于使用自己的证书重新打包SSL通信的代理后面,则需要将该证书添加到请求使用的证书(请求使用certifi来了解要信任的证书,而不是操作系统证书)。

您可以通过将环境变量"REQUESTS_CA_BUNDLE"设置为包含所需代理证书的包来完成此操作。

让pip工作的另一种方法是通过使用每个主机使用标志"--trusted-host"来告诉你信任主机的pip,而不管坏的证书,绕过安全性。

1
python get-pip.py --trusted-host=pypi.org --trusted-host=...

您可能不得不重复此步骤,因为pip在第一次SSL验证错误时失败,所以每次都会失败,直到添加完所有错误


以下命令可以解决此问题:

1
python get-pip.py --trusted-host=files.pythonhosted.org --trusted-host=pypi.org

在Windows中以管理员身份运行


每当我需要安装或更新我必须放置的东西时:

1
pip install --trusted-host=pypi.org --trusted-host=files.pythonhosted.org --user {name of whatever I'm installing}

我在这里找到了这个解决方案:https://github.com/pypa/pip/issues/5363


我下载了2.7

跑下面的代码,它的工作原理。我还没有将它插入到SQL表中

1
2
3
4
5
6
7
  import urllib, json
  import re
  url ="http://nagiosdatagateway.vestas.net/esq/ITE1452552/logstash-
  2018.12.16/2/desc"

  response = urllib.urlopen(url)
  data = json.loads(response.read())
  print (json.dumps(data,indent = 2))