简体   繁体   中英

ImportError: No module named 'typing' when trying to install a package

I want to install pygame on my PC when I type pip in cmd I get this error:

  File "C:\Python34\Scripts\pip3-script.py", line 9, in <module>
    load_entry_point('pip==21.1', 'console_scripts', 'pip3')()
  File "C:\Python34\lib\site-packages\setuptools-18.1-py3.4.egg\pkg_resources\__
init__.py", line 558, in load_entry_point
  File "C:\Python34\lib\site-packages\setuptools-18.1-py3.4.egg\pkg_resources\__
init__.py", line 2682, in load_entry_point
  File "C:\Python34\lib\site-packages\setuptools-18.1-py3.4.egg\pkg_resources\__
init__.py", line 2355, in load
  File "C:\Python34\lib\site-packages\setuptools-18.1-py3.4.egg\pkg_resources\__
init__.py", line 2361, in resolve
  File "C:\Users\user\AppData\Roaming\Python\Python34\site-packages\pip\__init__
.py", line 1, in <module>
    from typing import List, Optional
ImportError: No module named 'typing'

If you accidentally upgraded your pip, you are unfortunately unable to use pip to restore your previous version as pip is broken.

If you were on a Linux like Centos you maybe able to reinstall pip using this:

yum reinstall -y python2-pip.noarch python27-python-pip.noarch

But since you are using Windows, maybe try reinstall pip using https://bootstrap.pypa.io/get-pip.py and specify the pip version you want:

https://bootstrap.pypa.io/get-pip.py

pip itself is failing as it tries to import typing and typing is not installed. so you cannot run pip install to fix this.

TLDR; 1. use dpkg or yum to install typing (BUT this might not solve the issue with broken pip.

TLDR; 2. I think the problem is a python2.7/python3.x version issue, maybe just use python3.

A similar problem (with same fail to import typing module error) happened me on centos 7.9 after pip was upgraded. Solved for me after a while by just moving to use python3.

pip suggested this:

You are using pip version 8.1.2, however version 21.1.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.

I somewhat foolishly rhan that command.

Beware, don't always upgrade things as suggested!

After that pip upgrade pip was broken:

$ pip install httpserver
Traceback (most recent call last):
  File "/bin/pip2", line 7, in <module>
    from pip._internal.cli.main import main
  File "/usr/lib/python2.7/site-packages/pip/__init__.py", line 1, in <module>
    from typing import List, Optional
ImportError: No module named typing

So, we can try install the python modules otherwise, eg through package manager.

yum install python-typing

That installs fine however another error appears if pip is run.

pip install httpserver
Traceback (most recent call last):
  File "/bin/pip", line 7, in <module>
    from pip._internal.cli.main import main
  File "/usr/lib/python2.7/site-packages/pip/_internal/cli/main.py", line 58
    sys.stderr.write(f"ERROR: {exc}")
                                   ^
SyntaxError: invalid syntax

So, the real problem might be that some python packages are upgraded to python3.? syntax and pip is running python2, some kind of version mismatch.

So... well, the solution will vary.

In my case I found that http.server is a python3 only object. python2.7 was the only python installed on this box. The script I wanted to run required python3.

So a workaround for the probhlem is to just use python3:

# check python/pip related packages yum installed:
rpm -qa |grep -E "python|pip"
# install python 3
yum install python3
# now pip3 works, install some libs
pip3 install chevron uuid requests

Python 3 is now installed and working and pip3 for python 3 is working.

After this plain old pip for python2.7 is still broken.

Suggested solution left as an exercise for someone else/the future: Maybe we could fix by completely uninstalling pip and installing an old version again. Please do edit/update answer or add your own if you find out how to fix poor pip.

Do pip install typing then retry.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM