Ubuntu 下Python pip3安装及问题AttributeError: module 'pip.__main__' has no attribute '_main'

先卸载掉原先的pip3

sudo apt-get remove python3-pip

再重新安装

sudo apt-get install python3-pip

测试:输入 pip3 -V之后出现问题

Traceback (most recent call last):
  File "/usr/bin/pip3", line 9, in <module>
    from pip import main
ImportError: cannot import name 'main'

解决:

sudo gedit /usr/bin/pip3

把下面的三行

from pip import main
if name == ‘main’:
sys.exit(main())

换成下面的三行

from pip import main
if name == ‘main’:
sys.exit(main._main())

然后保存:source /usr/bin/pip3
继续查看版本:pip3 -V
得到:

pip 10.0.1 from /usr/local/lib/python2.7/dist-packages/pip (python 3.5)

这明显是有问题的,版本问题
再继续尝试:

yxh@yxh-NUC7i5BNH:~$ sudo pip3 -V
Traceback (most recent call last):
  File "/usr/bin/pip3", line 11, in <module>
    sys.exit(__main__._main())
AttributeError: module 'pip.__main__' has no attribute '_main'

不死心,想升级:

扫描二维码关注公众号,回复: 3669494 查看本文章
yxh@yxh-NUC7i5BNH:~$ pip3 install --upgrade pip 
Collecting pip
  Using cached https://files.pythonhosted.org/packages/c2/d7/90f34cb0d83a6c5631cf71dfe64cc1054598c843a92b400e55675cc2ac37/pip-18.1-py2.py3-none-any.whl
Installing collected packages: pip
  Found existing installation: pip 10.0.1
    Uninstalling pip-10.0.1:
Could not install packages due to an EnvironmentError: [Errno 13] Permission denied: '/usr/bin/pip'
Consider using the `--user` option or check the permissions.

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

权限问题需要用sudo或是–user,重点问题就在这里,我尝试过很多次都是用sudo来升级的,每次都是一样的问题无法解决,网络上也找不到解决办法,如下

yxh@yxh-NUC7i5BNH:~$ sudo pip3 install --upgrade pip 
Traceback (most recent call last):
  File "/usr/bin/pip3", line 11, in <module>
    sys.exit(__main__._main())
AttributeError: module 'pip.__main__' has no attribute '_main'

于是尝试用–user

yxh@yxh-NUC7i5BNH:~$ pip3 install --user --upgrade pip 
Collecting pip
  Using cached https://files.pythonhosted.org/packages/c2/d7/90f34cb0d83a6c5631cf71dfe64cc1054598c843a92b400e55675cc2ac37/pip-18.1-py2.py3-none-any.whl
Installing collected packages: pip
Successfully installed pip-18.1
You are using pip version 10.0.1, however version 18.1 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.

显示已经升级为18.1了那么我们来看下版本吧

yxh@yxh-NUC7i5BNH:~$ pip3 -V
pip 10.0.1 from /usr/local/lib/python2.7/dist-packages/pip (python 3.5)

什么鬼啊,难道要绝望了吗
幸好印象里记得看过这篇文章:https://segmentfault.com/q/1010000015052753?utm_source=tag-newest
所以决定再试一下

yxh@yxh-NUC7i5BNH:~$ pip -V
pip 10.0.1 from /usr/local/lib/python2.7/dist-packages/pip (python 2.7)
yxh@yxh-NUC7i5BNH:~$ sudo pip -V
pip 10.0.1 from /usr/local/lib/python2.7/dist-packages/pip (python 2.7)
yxh@yxh-NUC7i5BNH:~$ sudo pip3 -V
pip 18.1 from /home/yxh/.local/lib/python3.5/site-packages/pip (python 3.5)

终于没有问题了!!

最后还有点问题

yxh@yxh-NUC7i5BNH:~$ pip3 install --user matplotlib
bash: /usr/bin/pip3: No such file or directory

解决:

yxh@yxh-NUC7i5BNH:~$ which pip3
/home/yxh/.local/bin/pip3
yxh@yxh-NUC7i5BNH:~$ echo $PATH
/opt/ros/kinetic/bin:/home/yxh/anaconda2/env/py3/bin:/home/yxh/anaconda2/env/tensorflow/bin:/home/yxh/anaconda2/bin:/usr/bin/python:/home/yxh/bin:/home/yxh/.local/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
yxh@yxh-NUC7i5BNH:~$ sudo gedit ~/.bashrc

在里面将

export PAHT="/usr/local/bin:$PATH"

加入即可

参考文档:
https://blog.csdn.net/cow66/article/details/80069309
https://blog.csdn.net/JJandYY135/article/details/83060339
https://blog.csdn.net/accumulate_zhang/article/details/80269313?utm_source=copy
https://blog.csdn.net/weixin_39750084/article/details/81813949
https://blog.csdn.net/keith_bb/article/details/65435337
https://segmentfault.com/q/1010000015052753?utm_source=tag-newest
https://stackoverflow.com/questions/49836676/error-after-upgrading-pip-cannot-import-name-main

猜你喜欢

转载自blog.csdn.net/qq_17130909/article/details/83182395