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

本文详细记录了解决Ubuntu系统中pip3版本冲突的过程,包括卸载旧版本、安装新版本、解决版本显示错误及权限问题,最终实现正确版本的pip3安装与使用。

先卸载掉原先的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'

不死心,想升级:

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.youkuaiyun.com/cow66/article/details/80069309
https://blog.youkuaiyun.com/JJandYY135/article/details/83060339
https://blog.youkuaiyun.com/accumulate_zhang/article/details/80269313?utm_source=copy
https://blog.youkuaiyun.com/weixin_39750084/article/details/81813949
https://blog.youkuaiyun.com/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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值