1. 程式人生 > >Ubuntu 下Python pip3安裝及問題AttributeError: module 'pip.__main__' has no attribute '_main'

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)

這明顯是有問題的,版本問題
再繼續嘗試:

[email protected]:~$ 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'

不死心,想升級:

[email protected]:~$ 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來升級的,每次都是一樣的問題無法解決,網路上也找不到解決辦法,如下

[email protected]:~$ 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

[email protected]:~$ 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了那麼我們來看下版本吧

[email protected]:~$ pip3 -V
pip 10.0.1 from /usr/local/lib/python2.7/dist-packages/pip (python 3.5)

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

終於沒有問題了!!

最後還有點問題

[email protected]:~$ pip3 install --user matplotlib
bash: /usr/bin/pip3: No such file or directory

解決:

[email protected]:~$ which pip3
/home/yxh/.local/bin/pip3
[email protected]:~$ 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
[email protected]:~$ sudo gedit ~/.bashrc

在裡面將

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

加入即可