Troubleshooting python requests installation on OS X

0

Attempts to execute example flightaware python script fails:

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

Attempt to install requests via pip fails:

# 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

QUESTION

Is the SSL Certificate preventing the requests install?

Apparently the Mac has two versions of Python installed:

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

Attempt to install via 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.

Attempt to run python script:

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

Questions:

  1. Is the execution of the .py file performed by Python 2.7 or 3?
  2. How can the root cause / corrective action be diagnosed for the pip install failures?

UPDATES

Success running python script with python3

python3 flightawareXML01.py

Python 2.7 openSSL query results:

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

gatorback

Posted 2018-05-01T17:10:20.830

Reputation: 741

Answers

1

Is the execution of the .py file performed by Python 2.7 or 3?

Since you installed requests successfully with pip3 and python flightawareXML01.py indicates requests still isn't found, it seems reasonable to assume your python command is attempting to execute your .py file with Python 2.7 (you can always use python -V to double-check the version).

To resolve this, try using python3 rather than python (i.e. python3 flightawareXML01.py).

Is the SSL Certificate preventing the requests install?

It seems likely this isn't a certificate issue in and of itself (since the current SSL certificate for https://pypi.python.org/simple/requests/ seems valid) but the error does seem to relate to SSL.

How can the root cause / corrective action be diagnosed for the pip install failures?

The error you received:

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

didn't seem to have a definite conclusion from what I could tell, other than the wrong SSL protocol versions or ciphers possibly being used between the client (pip) and the server (based on Googling around tlsv1 alert protocol version).

That said, you might be interested in this StackOverflow question, as well as this one. These both propose remedies for this issue (though, admittedly, the answers seem mostly to revolve around upgrading OpenSSL/Python).

Anaksunaman

Posted 2018-05-01T17:10:20.830

Reputation: 9 278