Error after upgrading pip: cannot import name 'main'

后端 未结 30 2036
清歌不尽
清歌不尽 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:08

    Use python -m pip install instead of pip install

    Example:

    python -m pip install --user somepackage
    python3 -m pip install --user somepackage
    

    The pip (resp. pip3) executable is provided by your distro (python-pip package on Ubuntu 16.04) and located at /usr/bin/pip.

    Therefore, it is not kept up-to date with the pip package itself as you upgrade pip, and may break.

    If you just use python -m pip directly, e.g. as in:

    python -m pip install --user somepackage
    python3 -m pip install --user somepackage
    

    it goes through your Python path, finds the latest version of pip and executes that file.

    It relies on the fact that file is executable through import, but that is a very standard type of interface, and therefore less likely to break than the hackier Debian script.

    Then I recommend adding the following aliases to your .bashrc:

    pip() ( python -m pip "$@" )
    pip3() ( python3 -m pip "$@" )
    

    The Ubuntu 18.04 /usr/bin/pip3 file does:

    from pip import main
    

    and presumably main was removed from pip at some point which is what broke things.

    The breaking pip commit appears to be: 95bcf8c5f6394298035a7332c441868f3b0169f4 "Move all internal APIs to pip._internal" which went into pip 18.0.

    Tested in Ubuntu 16.04 after an update from pip3 9.0.1 to 18.0.

    pyenv

    Ultimately however, for serious Python development I would just recommend that you install your own local Python with pyenv + virtualenv, which would also get around this Ubuntu bug: https://askubuntu.com/questions/682869/how-do-i-install-a-different-python-version-using-apt-get/1195153#1195153

提交回复
热议问题