Error after upgrading pip: cannot import name '

2019-01-02 21: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 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.

标签: python pip
19条回答
我只想做你的唯一
2楼-- · 2019-01-02 22:14

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

查看更多
贼婆χ
3楼-- · 2019-01-02 22:15

For Ubuntu family, Debian, Linux Mint users

Thanks to Anthony's explanation above, you can retain your original system pip (in /usr/bin/ and dist-packages/) and remove the manually-installed pip (in ~/.local/) to resolve the conflict:

$ python3 -m pip uninstall pip

Ubuntu/Debian pip v8.1.1 (16.04) from python3-pip debian package (see$ pip3 -V) shows the same search results as the latest pip v10.0.1, and installs latest modules from PyPI just fine. It has a working pip command (already in the $PATH), plus the nice --user option patched-in by default since 2016. Looking at pip release notes, the newer versions are mostly about use-case specific bug fixes and certain new features, so not everyone has to rush upgrading pip just yet. And the new pip 10 can be deployed to Python virtualenvs, anyway.

But regardless of pips, your OS allows to quickly install common Python modules (including numpy) with APT, without the need for pip, for example:
$ sudo apt install python3-numpy python3-scipy (with system dependencies)
$ sudo apt install python3-pip (Debian-patched pip, slightly older but it doesn't matter)

Quick apt syntax reminder (please see man apt for details):
$ sudo apt update (to resync Ubuntu package index files from up-to-date sources)
$ apt search <python-package-name> (full text-search on all available packages)
$ apt show <python-package-name> (displays the detailed package description)
$ sudo apt install <python-package-name>

Package names prefixed with python- are for Python 2; and prefixed with python3- are for Python 3 (e.g. python3-pandas). There are thousands, and they undergo integration testing within Debian and Ubuntu. Unless you seek to install at per-user level (pip install --user option) or within virtualenv/venv, apt could be what you needed. These system packages are accessible from virtual envs too, as virtualenv will gracefully fall back to using system libs on import if your envs don't have given copies of modules. Your custom-installed (with pip --user) per-user modules in ~/.local/lib will override them too.

Note, since this is a system-wide installation, you'd rarely need to remove them (need to be mindful about OS dependencies). This is convenient for packages with many system dependencies (such as with scipy or matplotlib), as APT will keep track and provide all required system libs and C extensions, while with pip you have no such guarantees.

In fact, for system-wide Python packages (in contrast to per-user, home dir level, or lower), Ubuntu expects using the APT package manager (rather than sudo pip) to avoid breaking OS: sudo pip3 targets the very same /usr/lib/python3/dist-packages directory where APT stores OS-sensitive modules. Recent Debian/Ubuntu releases depend heavily on Python 3, so its pre-installed modules are managed by apt and shouldn't be changed.

So if you use pip3 install command, please ensure that it runs in an isolated virtual dev environment, such as with virtualenv (sudo apt install python3-virtualenv), or with Python3 built-in (-m venv), or at a per-user level (--user pip option, default in Ubuntu-provided pip since 2016), but not system-wide (never sudo pip3!), because pip interferes with the operation of the APT package manager and may affect Ubuntu OS components when a system-used python module is unexpectedly changed. Good luck!


P. S. All the above is for the 'ideal' solution (Debian/Ubuntu way).

If you still want to use the new pip3 v10 exclusively, there are 3 quick workarounds:

  • simply open a new bash session (a new terminal tab, or type bash) - and pip3 v10 becomes available (see pip3 -V). debian's pip3 v8 remains installed but is broken; or
  • the command $ hash -d pip3 && pip3 -V to refresh pip3 pathname in the $PATH. debian's pip3 v8 remains installed but is broken; or
  • the command $ sudo apt remove python3-pip && hash -d pip3 to uninstall debian's pip3 v8 completely, in favor of your new pip3 v10.

Note: You will always need to add --user flag to any non-debian-provided pip, unless you are in a virtualenv! (it deploys python packages to ~/.local/, default in debian/ubuntu-provided python3-pip and python-pip since 2016). Your use of pip 10 system-wide, outside of virtualenv, is not really supported by Ubuntu/Debian. Never sudo pip3!

Further details:
https://github.com/pypa/pip/issues/5221#issuecomment-382069604
https://github.com/pypa/pip/issues/5240#issuecomment-381673100

查看更多
forever°为你锁心
4楼-- · 2019-01-02 22:17

The commands above didn't work for me but those were very helpful:

sudo apt purge python3-pip
sudo rm -rf '/usr/lib/python3/dist-packages/pip'  
sudo apt install python3-pip
cd
cd .local/lib/python3/site-packages
sudo rm -rf pip*  
cd
cd .local/lib/python3.5/site-packages
sudo rm -rf pip*  
sudo pip3 install jupyter
查看更多
成全新的幸福
5楼-- · 2019-01-02 22:20

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

查看更多
啃猪蹄的小仙女
6楼-- · 2019-01-02 22:21

You can resolve this issue by reinstalling pip.

Use the following code to reinstall pip

For python2:

python -m pip uninstall pip && sudo apt install python-pip --reinstall

For python2:

 python3 -m pip uninstall pip && sudo apt install python3-pip --reinstall
查看更多
来,给爷笑一个
7楼-- · 2019-01-02 22:22

I'm running on a system where I have sudo apt but no sudo pip. (And no su access.) I got myself into this same situation by following the advice from pip:

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

None of the other fixes worked for me, because I don't have enough admin privileges. However, a few things stuck with me from reading up on this:

  • I shouldn't have done this. Sure, pip told me to. It lied.
  • Using --user solves a lot of issues by focusing on the user-only directory.

So, I found this command line to work to revert me back to where I was. If you were using a different version than 8.1.1, you will obviously want to change that part of the line.

python -m pip install --force-reinstall pip==8.1.1 --user

That's the only thing that worked for me, but it worked perfectly!

查看更多
登录 后发表回答