最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】Ubuntu升级pip后pip3 install报错:ImportError cannot import name main

报错 crifan 1417浏览 0评论
折腾:
【未解决】本地搭建和部署运行Django项目
期间,Ubuntu中升级了pip3后,去运行pip3,结果报错:
# pip3 install pipenv
Traceback (most recent call last):
  File "/usr/bin/pip3", line 9, in <module>
    from pip import main
ImportError: cannot import name 'main'
去看看:
# cat /usr/bin/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())

# ll /usr/bin/python3
lrwxrwxrwx 1 root root 9 Oct 26  2016 /usr/bin/python3 -> python3.5*
结果:
ubuntu python ImportError: cannot import name ‘main’
升级pip后出现ImportError: cannot import name main – accumulate_zhang的博客 – CSDN博客
pip升级后Import Error:cannot import name main解决方案 – ZONGXP的博客 – CSDN博客
pip Import Error:cannot import name main解决方案 – 干巴他爹的小本本 – CSDN博客
ubuntu升级pip后, ImportError: cannot import name ‘main‘ – hello-Jesson – 博客园
# which pip3
/usr/local/bin/pip3
然后发现:
root@VM-158-80-ubuntu:~/xxx/xxx-backend# ll /usr/local/bin/pip
pip     pip3    pip3.5  
root@VM-158-80-ubuntu:~/xxx/xxx-backend# ll /usr/local/bin/pip*
-rwxr-xr-x 1 root root 215 Jun 11 21:03 /usr/local/bin/pip*
-rwxr-xr-x 1 root root 215 Jun 11 21:03 /usr/local/bin/pip3*
-rwxr-xr-x 1 root root 215 Jun 11 21:03 /usr/local/bin/pip3.5*
root@VM-158-80-ubuntu:~/xxx/xxx-backend# cat /usr/local/bin/pip3
#!/usr/bin/python3

# -*- coding: utf-8 -*-
import re
import sys

from pip._internal import main

if __name__ == '__main__':
    sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0])
    sys.exit(main())
就不一样。
那就先去试试这个local bin下面的pip3能否用:
# /usr/local/bin/pip3 install pipenv
Collecting pipenv
  Downloading https://files.pythonhosted.org/packages/13/b4/3ffa55f77161cff9a5220f162670f7c5eb00df52e00939e203f601b0f579/pipenv-2018.11.26-py3-none-any.whl (5.2MB)
     |██████████████                  | 2.3MB 73kB/s eta 0:00:40
说明是可以的。
-》那就采用这个写法。
把之前的
/usr/bin/pip3
换成:
/usr/local/bin/pip3
-》不过,突然想到,应该去改PATH路径,更靠谱
# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin
已经是:
/usr/local/bin
/usr/bin
之前了啊,
估计是当前shell问题?
断开ssh连接,重新ssh登录,结果再去
# pip3 install pipenv
Requirement already satisfied: pipenv in /usr/local/lib/python3.5/dist-packages (2018.11.26)
Requirement already satisfied: certifi in /usr/local/lib/python3.5/dist-packages (from pipenv) (2019.3.9)
Requirement already satisfied: pip>=9.0.1 in /usr/local/lib/python3.5/dist-packages (from pipenv) (19.1.1)
Requirement already satisfied: virtualenv in /usr/local/lib/python3.5/dist-packages (from pipenv) (16.6.0)
Requirement already satisfied: virtualenv-clone>=0.2.5 in /usr/local/lib/python3.5/dist-packages (from pipenv) (0.5.3)
Requirement already satisfied: setuptools>=36.2.1 in /usr/local/lib/python3.5/dist-packages (from pipenv) (41.0.1)
没有报错。说明安装成功了。
说明前面错误没了。
那再去随便安装一个其他的库试试pip3是否正常了。
不够想起来直接去加上路径,即可测试是否工作:
# /usr/bin/pip3 --version
Traceback (most recent call last):
  File "/usr/bin/pip3", line 9, in <module>
    from pip import main
ImportError: cannot import name 'main'
说明旧的还是有问题。
但是此处已经由PATH决定,用的pip3的是正常的了。
【总结】
Ubuntu中,从旧版本pip3升级到最新版后,去用pip3会报错:
ImportError: cannot import name 'main'
原因:
当前shell中环境还是旧的
-》虽然用which去找,的确是最新版本了
# which pip3
/usr/local/bin/pip3
但是实际上调用的还是旧的版本,从而报错。
解决办法:
关闭当前shell
-》此处是通过SecureCRT的ssh远程登录到Ubuntu,则是关闭连接,重新打开连接即可。
附录:
新版本pip3:
# cat /usr/local/bin/pip3
#!/usr/bin/python3

# -*- coding: utf-8 -*-
import re
import sys

from pip._internal import main

if __name__ == '__main__':
    sys.argv[0] = re.sub(r'(-script\.pyw|\.exe)?$', '', sys.argv[0])
    sys.exit(main())
旧版本pip3:
# cat /usr/bin/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())

转载请注明:在路上 » 【已解决】Ubuntu升级pip后pip3 install报错:ImportError cannot import name main

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
80 queries in 0.176 seconds, using 22.12MB memory