0
votes

I'm trying to create a python2 virtualenv, so I try pip install virtualenv and get

Traceback (most recent call last):
  File "/usr/local/bin/pip", line 11, in <module>
    load_entry_point('pip==21.1.1', 'console_scripts', 'pip')()
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/pkg_resources/__init__.py", line 489, in load_entry_point
    return get_distribution(dist).load_entry_point(group, name)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/pkg_resources/__init__.py", line 2843, in load_entry_point
    return ep.load()
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/pkg_resources/__init__.py", line 2434, in load
    return self.resolve()
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/pkg_resources/__init__.py", line 2440, in resolve
    module = __import__(self.module_name, fromlist=['__name__'], level=0)
  File "/Library/Python/2.7/site-packages/pip-21.1.1-py2.7.egg/pip/__init__.py", line 1, in <module>
    from typing import List, Optional
ImportError: No module named typing

I get an almost identical error with pip install typing

Traceback (most recent call last):
  File "/usr/local/bin/pip", line 11, in <module>
    load_entry_point('pip==21.1.1', 'console_scripts', 'pip')()
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/pkg_resources/__init__.py", line 489, in load_entry_point
    return get_distribution(dist).load_entry_point(group, name)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/pkg_resources/__init__.py", line 2843, in load_entry_point
    return ep.load()
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/pkg_resources/__init__.py", line 2434, in load
    return self.resolve()
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/pkg_resources/__init__.py", line 2440, in resolve
    module = __import__(self.module_name, fromlist=['__name__'], level=0)
  File "/Library/Python/2.7/site-packages/pip-21.1.1-py2.7.egg/pip/__init__.py", line 1, in <module>
    from typing import List, Optional
ImportError: No module named typing

A lot of solutions suggest using python3 but I'm specifically trying to create a python 2 virtual environment


I'm running MacOS Big Sur on a 2018 Intel i3 Mac Mini

5
I don't know who downvoted all 3 answers, but it most likely would deter people from leaving more answers if they saw all answers downvoted - Sam
All answers at that point focused on the wrong thing, not solving your problem. - deceze♦
You need to downgrade pip for your Python. See stackoverflow.com/a/65871131/7976758 . Your problem is different but the process of downgrading and URLs are the same. - phd

5 Answers

2
votes

Python 2 reached its end of life (EOL). Consequently the support for Python 2 was removed from pip in version 21. You need to downgrade pip to version 20.


I tried this (in shell command line):

# downgrade pip to 20.3.4
pip install --user pip==20.3.4
# better: 'pip<21', but it must be quoted!

# install virtualenv if not installed already
pip install --user virtualenv

# create a new venv for old python
virtualenv -p /usr/bin/python2.7 /tmp/venv27

# update pip back to the recent version
pip install --user --upgrade pip

Now, pip --version shows version 21, but inside the activated environment there is version 20:

sh-5.0$ pip --version
pip 21.1.1 from /home/vpfb/.local/lib/python3.9/site-packages/pip (python 3.9)

sh-5.0$ pwd
/tmp/venv27/bin

sh-5.0$ source ./activate

(venv27) sh-5.0$ pip --version
pip 20.3.4 from /tmp/venv27/lib/python2.7/site-packages/pip (python 2.7)

I tried to install a random package in the venv and it succeeded.

0
votes

I'm afraid you'll have to use another version. I know you've been hearing this a lot, but python 2.7 isn't supported anymore (since 1st January 2021) so thats why it might be showing an error.

0
votes

typing module is only valid for python version 3.5 and above.

https://pypi.org/project/typing/

0
votes

typing is

New in version 3.5.

So it should be no surprise that import typing caused failure in python2.7. So if you find project using import typing is either for python at least 3.5 or is using typing different from built-in module. If you know to want ramifications of python2 end of life read Sunsetting Python 2. If you have legacy python2 you might try porting to python3

-1
votes

pip stopped supporting Python 2 in version 21. You’ll need to install an older version of pip for Python 2.