问题

问题

尝试执行示例flightaware python 脚本失败:

Users-MacBook-Air:FlightAwareXML3 user$ python flightawareXML01.py
Traceback (most recent call last):
  File "flightawareXML01.py", line 9, in <module> import requests   

尝试安装要求通过 pip失败:

# sudo pip install requests
ImportError: No module named requests
Users-MacBook-Air:FlightAwareXML3 user$ sudo pip install requests
Password:
The directory '/Users/user/Library/Caches/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
The directory '/Users/user/Library/Caches/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Collecting requests
  Could not fetch URL https://pypi.python.org/simple/requests/: There was a problem confirming the ssl certificate: [SSL: TLSV1_ALERT_PROTOCOL_VERSION] tlsv1 alert protocol version (_ssl.c:590) - skipping
  Could not find a version that satisfies the requirement requests (from versions: )
No matching distribution found for requests
Users-MacBook-Air:FlightAwareXML3 user$ pip --version

问题

SSL 证书是否会阻止要求安装?

显然 Mac 安装了两个版本的 Python:

pip 9.0.1 from /Library/Python/2.7/site-packages (python 2.7)
Users-MacBook-Air:FlightAwareXML3 user$ pip3 --version
pip 8.0.2 from /usr/local/lib/python3.5/site-packages (python 3.5)
Users-MacBook-Air:FlightAwareXML3 user$ which python
/usr/bin/python

尝试通过 pip3 安装:

Users-MacBook-Air:FlightAwareXML3 user$ sudo pip3 install requests
Password:
The directory '/Users/user/Library/Caches/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
The directory '/Users/user/Library/Caches/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Collecting requests
  Downloading https://files.pythonhosted.org/packages/49/df/50aa1999ab9bde74656c2919d9c0c085fd2b3775fd3eca826012bef76d8c/requests-2.18.4-py2.py3-none-any.whl (88kB)
    100% |████████████████████████████████| 90kB 94kB/s 
Collecting chardet<3.1.0,>=3.0.2 (from requests)
  Downloading https://files.pythonhosted.org/packages/bc/a9/01ffebfb562e4274b6487b4bb1ddec7ca55ec7510b22e4c51f14098443b8/chardet-3.0.4-py2.py3-none-any.whl (133kB)
    100% |████████████████████████████████| 135kB 102kB/s 
Collecting certifi>=2017.4.17 (from requests)
  Downloading https://files.pythonhosted.org/packages/7c/e6/92ad559b7192d846975fc916b65f667c7b8c3a32bea7372340bfe9a15fa5/certifi-2018.4.16-py2.py3-none-any.whl (150kB)
    100% |████████████████████████████████| 151kB 130kB/s 
Collecting idna<2.7,>=2.5 (from requests)
  Downloading https://files.pythonhosted.org/packages/27/cc/6dd9a3869f15c2edfab863b992838277279ce92663d334df9ecf5106f5c6/idna-2.6-py2.py3-none-any.whl (56kB)
    100% |████████████████████████████████| 57kB 137kB/s 
Collecting urllib3<1.23,>=1.21.1 (from requests)
  Downloading https://files.pythonhosted.org/packages/63/cb/6965947c13a94236f6d4b8223e21beb4d576dc72e8130bd7880f600839b8/urllib3-1.22-py2.py3-none-any.whl (132kB)
    100% |████████████████████████████████| 135kB 163kB/s 
Installing collected packages: chardet, certifi, idna, urllib3, requests
Successfully installed certifi-2018.4.16 chardet-3.0.4 idna-2.6 requests-2.18.4 urllib3-1.22
You are using pip version 8.0.2, however version 10.0.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.

尝试运行python脚本:

Users-MacBook-Air:FlightAwareXML3 user$ python flightawareXML01.py
Traceback (most recent call last):
  File "flightawareXML01.py", line 9, in <module>
    import requests   # sudo pip install requests
ImportError: No module named requests

问题:

  1. .py 文件的执行是由 Python 2.7 还是 3 执行的?
  2. 如何诊断 pip 安装失败的根本原因/纠正措施?

更新

使用 python3 成功运行 python 脚本

python3 flightawareXML01.py

Python 2.7 openSSL 查询结果:

Users-MacBook-Air:FlightAwareXML3 user$ python3 -c "import ssl; print(ssl.OPENSSL_VERSION)"
OpenSSL 1.0.2g  1 Mar 2016
Users-MacBook-Air:FlightAwareXML3 user$ python -c "import ssl; print(ssl.OPENSSL_VERSION)"
OpenSSL 0.9.8zh 14 Jan 2016

答案1

.py 文件的执行是由 Python 2.7 还是 3 执行的?

由于您requests已成功安装pip3python flightawareXML01.py仍然requests未找到,因此可以合理地假设您的python命令正在尝试.py使用 Python 2.7 执行您的文件(您可以随时使用python -V它来仔细检查版本)。

为了解决此问题,请尝试使用python3而不是python(即python3 flightawareXML01.py)。

SSL 证书是否阻止requests安装?

这似乎本身不是一个证书问题(因为当前的 SSL 证书似乎https://pypi.python.org/simple/requests/有效)但错误似乎与 SSL 有关。

如何诊断pip install故障的根本原因/纠正措施?

您收到的错误:

无法获取 URLhttps://pypi.python.org/simple/requests/:确认 SSL 证书时出现问题:[SSL:TLSV1_ALERT_PROTOCOL_VERSION] tlsv1 警报协议版本 (_ssl.c:590) - 跳过

据我所知,似乎没有明确的结论,除了客户端(pip)和服务器之间可能使用了错误的 SSL 协议版本或密码(基于谷歌搜索tlsv1 alert protocol version)。

话虽如此,你可能对此感兴趣StackOverflow 问题, 也这个。这两个都提出了针对此问题的补救措施(尽管,不可否认,答案似乎主要围绕升级 OpenSSL/Python)。

相关内容