Error after upgrading pip: cannot import name 'main'

后端 未结 30 1692
清歌不尽
清歌不尽 2020-11-22 15:51

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 l         


        
相关标签:
30条回答
  • 2020-11-22 16:09

    I use sudo apt remove python3-pip then pip works.

     ~ sudo pip install pip --upgrade
    [sudo] password for sen: 
    Traceback (most recent call last):
      File "/usr/bin/pip", line 9, in <module>
        from pip import main
    ImportError: cannot import name 'main'
    ➜  ~ sudo apt remove python3-pip   
    Reading package lists... Done
    Building dependency tree       
    Reading state information... Done
    The following packages were automatically installed and are no longer required:
      libexpat1-dev libpython3-dev libpython3.5-dev python-pip-whl python3-dev python3-wheel
      python3.5-dev
    Use 'sudo apt autoremove' to remove them.
    The following packages will be REMOVED:
      python3-pip
    0 upgraded, 0 newly installed, 1 to remove and 0 not upgraded.
    After this operation, 569 kB disk space will be freed.
    Do you want to continue? [Y/n] y
    (Reading database ... 215769 files and directories currently installed.)
    Removing python3-pip (8.1.1-2ubuntu0.4) ...
    Processing triggers for man-db (2.7.5-1) ...
    ➜  ~ pip
    
    Usage:   
      pip <command> [options]
    
    0 讨论(0)
  • 2020-11-22 16:10

    Recover with python3 -m pip install --user pip==9.0.1 (or the version that worked)

    0 讨论(0)
  • 2020-11-22 16:12

    Check if pip has been cached on another path, to do so, call $ which pip and check that the path is different from the one prompted in the error, if that's the case run:

    $ hash -r
    

    When the cache is clear, pip will be working again. reference: http://cheng.logdown.com/posts/2015/06/14/-usr-bin-pip-no-such-file-or-directory

    0 讨论(0)
  • 2020-11-22 16:12

    For what it's worth, I had the problem with pip (not pip2 or pip3):

    $ pip -V
    Traceback (most recent call last):
      File "/usr/bin/pip", line 9, in <module>
        from pip import main
    ImportError: cannot import name main
    
    $ pip2 -V
    pip 8.1.1 from /usr/lib/python2.7/dist-packages (python 2.7)
    
    $ pip3 -V
    pip 8.1.1 from /usr/lib/python3/dist-packages (python 3.5)
    

    Somehow (I can't remember how) I had python stuff installed in my ~/.local directory. After I removed the pip directory from there, pip started working again.

    $ rm -rf /home/precor/.local/lib/python2.7/site-packages/pip
    $ pip -V
    pip 8.1.1 from /usr/lib/python2.7/dist-packages (python 2.7)
    
    0 讨论(0)
  • 2020-11-22 16:13

    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.

    0 讨论(0)
  • 2020-11-22 16:14

    I also run into this problem when I wanted to upgrade system pip pip3 from 9.0.1 to 19.2.3.

    After running pip3 install --upgrade pip, pip version becomes 19.2.3. But main() has been moved in pip._internal in the latest version, which leaves pip3 broken.

    So in file /usr/bin/pip3, replace line 9: from pip import main with from pip._internal import main. The issue will be fixed, works the same for python2-pip. (Tested on Ubuntu 18.04 distribution)

    According to @Vincent H.'s answer

    0 讨论(0)
提交回复
热议问题