Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error after upgrading pip: cannot import name 'main'

Tags:

python

pip

Whenever I am trying to install any package using pip, I am getting this import error:

guru@guru-notebook:~$ pip3 install numpy Traceback (most recent call last):   File "/usr/bin/pip3", line 9, in <module>     from pip import main ImportError: cannot import name 'main' 


guru@guru-notebook:~$ cat `which pip3` #!/usr/bin/python3 # GENERATED BY DEBIAN  import sys  # Run the main entry point, similarly to how setuptools does it, but because # we didn't install the actual entry point from setup.py, don't use the # pkg_resources API. from pip import main if __name__ == '__main__':     sys.exit(main()) 

It was working fine earlier, I am not sure why it is throwing this error. I have searched about this error, but can't find anything to fix it.

Please let me know if you need any further detail, I will update my question.

like image 850
g_p Avatar asked Apr 14 '18 22:04

g_p


People also ask

Can not import name Python?

The python ImportError: cannot import name error occurs when the import class is inaccessible or the imported class in circular dependence. The import keyword is used to load class and function. The keyword from is used to load the module.

How do I upgrade pip to a specific version?

Upgrade Or Downgrade To Specific Pip Version If you want to upgrade or downgrade your version of pip to a specific version on a Mac, you can do this by adding a pip==<version> flag to the end of your update command.


Video Answer


2 Answers

You must have inadvertently upgraded your system pip (probably through something like sudo pip install pip --upgrade)

pip 10.x adjusts where its internals are situated. The pip3 command you're seeing is one provided by your package maintainer (presumably debian based here?) and is not a file managed by pip.

You can read more about this on pip's issue tracker

You'll probably want to not upgrade your system pip and instead use a virtualenv.

To recover the pip3 binary you'll need to sudo python3 -m pip uninstall pip && sudo apt install python3-pip --reinstall.

If you want to continue in "unsupported territory" (upgrading a system package outside of the system package manager), you can probably get away with python3 -m pip ... instead of pip3.

like image 141
Anthony Sottile Avatar answered Sep 28 '22 22:09

Anthony Sottile


We can clear the error by modifying the pip file.

Check the location of the file:

$ which pip 

path -> /usr/bin/pip

Go to that location(/usr/bin/pip) and open terminal

Enter: $ sudo nano pip

You can see:

import sys from pip import main if __name__ == '__main__':      sys.exit(main()) 

Change to:

import sys from pip import __main__ if __name__ == '__main__':      sys.exit(__main__._main()) 

then ctrl + o write the changes and exit

Hope this will do!!

like image 21
vijay athithya Avatar answered Sep 28 '22 21:09

vijay athithya