Trying to get Python, Pip and Requests all up and running and failing to do so

Viewed 2283

I am running a Linux server (Linux 4.1.12-124.19.1.el7uek.x86_64 #2 SMP Wed Sep 5 13:41:16 PDT 2018 x86_64 x86_64 x86_64 GNU/Linux) with python 2.7.5 on it (DBAs want it to be 2.7.5 for their script) and trying to get pip and requests running.

I was having a problem getting pip on, because when I would try to install the epel-release, it would tell me "no package" named that. So I had to do

"yum install http://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm"

Which finally let me do

"yum install python-pip"

to get pip installed.

I then did

"python -m pip install requests"

to install requests.

It looked good, but when I run the test script that the DBAs gave me to check, it gives me this.

"Traceback (most recent call last):
  File "/mnt/EBS_Cloning_Repo/clone/scripts/test.py", line 2, in <module>
    import requests
  File "/usr/lib/python2.7/site-packages/requests/__init__.py", line 133, in <module>
    from . import utils
  File "/usr/lib/python2.7/site-packages/requests/utils.py", line 27, in <module>
    from . import certs
  File "/usr/lib/python2.7/site-packages/requests/certs.py", line 15, in <module>
    from certifi import where
  File "/usr/lib/python2.7/site-packages/certifi/__init__.py", line 1, in <module>
    from .core import contents, where
  File "/usr/lib/python2.7/site-packages/certifi/core.py", line 17
    def where() -> str:
                ^
SyntaxError: invalid syntax"

I did find that this could be caused by running just the python command, instead of one like python2, so I try redoing some of the commands with the python2 command with the same results.

I do have another server, running as it should with this, and I compared the files that it is pointing to, and seeing some missing information in them (diff output one request), but I am not sure what I can do with that information.

2 Answers

TLDR

python -m pip install requests 'certifi<=2020.4.5.1'

Analysis

  File "/usr/lib/python2.7/site-packages/certifi/core.py", line 17
    def where() -> str:
                ^
SyntaxError: invalid syntax"
  • It's using python 2.7

  • The -> str in def where() -> str is a type annotation

    • Python 3.0 introduced a function annotation syntax in PEP 3107

    • Python 3.5 officially adds standard definitions for them via PEP 484's typing module

    • Python 2.7 would definitely raise invalid syntax if it found an annotation

      test.py:

      class MyClass(object):
          def test(self) -> bool:
              return True
      

      Output:

      ❯ python ./test.py
        File "./test.py", line 2
        def test(self) -> bool:
                       ^
        SyntaxError: invalid syntax
      
  • certifi

    • Certifi dropped python 2.7 in 2020.04.05.2 (commit)
    • Certifi's last python 2.7 release is 2020.04.5 (github)
  • certifi is being implicitly fetch as a sub-dependency / sub-package

    • We can use pip's resolver to pin the dependency while installing

Example

Optional sandbox:

virtualenv --python python2.7 .venv
. .venv/bin/activate

Command:

python -m pip install requests 'certifi<=2020.4.5.1'

Output:

DEPRECATION: Python 2.7 reached the end of its life on January 1st, 2020. Please upgrade your Python as Python 2.7 is no longer maintained. pip 21.0 will drop support for Python 2.7 in January 2021. More details about Python 2 support in pip can be found at https://pip.pypa.io/en/latest/development/release-process/#python-2-support pip 21.0 will remove support for this functionality.
Collecting requests
  Downloading requests-2.27.1-py2.py3-none-any.whl (63 kB)
     |████████████████████████████████| 63 kB 599 kB/s
Collecting certifi<=2020.4.5.1
  Downloading certifi-2020.4.5.1-py2.py3-none-any.whl (157 kB)
     |████████████████████████████████| 157 kB 5.6 MB/s
Collecting urllib3<1.27,>=1.21.1
  Downloading urllib3-1.26.9-py2.py3-none-any.whl (138 kB)
     |████████████████████████████████| 138 kB 20.3 MB/s
Collecting chardet<5,>=3.0.2; python_version < "3"
  Downloading chardet-4.0.0-py2.py3-none-any.whl (178 kB)
     |████████████████████████████████| 178 kB 26.0 MB/s
Collecting idna<3,>=2.5; python_version < "3"
  Downloading idna-2.10-py2.py3-none-any.whl (58 kB)
     |████████████████████████████████| 58 kB 5.5 MB/s
Installing collected packages: certifi, urllib3, chardet, idna, requests
Successfully installed certifi-2020.4.5.1 chardet-4.0.0 idna-2.10 requests-2.27.1 urllib3-1.26.9

I think this is a compatibility bug. The pip is broken, I tried yum remove python2-pip and reinstalling it. I think the error-certified dependency makes it bad. So you can COPY the good certified dependency files to the destinated PC. The location is /usr/lib/python2.7/site-packages/.

Related