Issue
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
Solution
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.
Answered By - VPfB Answer Checked By - Terry (WPSolving Volunteer)