1

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

Sam
  • 1,765
  • 11
  • 82
  • 176
  • 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 May 23 '21 at 06:45
  • All answers at that point focused on the wrong thing, not solving your problem. – deceze May 23 '21 at 06:48
  • 1
    You need to downgrade pip for your Python. See https://stackoverflow.com/a/65871131/7976758/ . Your problem is different but the process of downgrading and URLs are the same. – phd May 23 '21 at 09:24

5 Answers5

4

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.

VPfB
  • 14,927
  • 6
  • 41
  • 75
  • How would you suggest doing this – Sam May 23 '21 at 06:49
  • with `pip==20.3.4` (latest pip 20.X.Y) argument as shown here: https://stackoverflow.com/questions/5226311/installing-specific-package-versions-with-pip – VPfB May 23 '21 at 07:03
  • Is there a way to do this all in a virtual environment? – Sam May 23 '21 at 08:01
  • When I try `pip install pip==20.3.4` I get the same error about typing – Sam May 23 '21 at 08:02
  • @Sam Since your pip is already “corrupted”, you can’t use it to install another version of it. You’ll need to install it from scratch some other way. Consult the documentation for the most appropriate method for your system. – deceze May 23 '21 at 08:29
  • So after removing pip inside `/usr/local/bin/pip` I ran `sudo easy_install pip==20.3.4` after this I was able to install `virtualenv`. When I tried using `pip install --user pip==20.3.4` , I got the same no module named typing error. Could you include this information in your answer? – Sam May 23 '21 at 08:46
  • @Sam I guess you might have multiple pips installed. Use the full path e.g. `/usr/local/bin/pip` and verify with `--version` before installing that you run the version you want. – VPfB May 23 '21 at 08:52
  • I think i just have pip and pip3, I got this computer a few weeks ago – Sam May 23 '21 at 08:55
  • @Sam maybe there are also other issues. Please update your question with more details to make it reproducible step by step: which commands did you run (with version info) to setup your environment and which command failed. – VPfB May 23 '21 at 09:22
0

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.

shreyas
  • 7
  • 2
  • 1
    What exactly does not supported mean? What I'm trying to do is run an [old Github project](https://github.com/ksallberg/wallstreet_tycoon) without changing anything. Python2 may not be supported, but is this still possible? – Sam May 23 '21 at 06:28
  • I dont think it will work anymore. Not supported means it simply does not work anymore. – shreyas May 23 '21 at 06:43
  • 1
    @shreyas That’s not what it means at all. The existing software will keep working fine as it always has. There’s no kill switch in it. It just means that no further updates will be released ever, and don’t-come-crying-to-us-if-something-doesn’t-work. – deceze May 23 '21 at 06:46
0

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

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

Izzat Z.
  • 437
  • 1
  • 7
  • 15
0

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

Daweo
  • 31,313
  • 3
  • 12
  • 25
  • Is there any tool that makes it easy to convert python2 code to python3 – Sam May 23 '21 at 06:54
  • @Sam You shouldn’t need to convert a project to Python 3 just to run it. Depending on the complexity of the project that can be a multi-month endeavor. – deceze May 23 '21 at 07:29
  • @Sam take look at [2to3](https://docs.python.org/3/library/2to3.html) – Daweo May 23 '21 at 07:41
0

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

deceze
  • 510,633
  • 85
  • 743
  • 889