多Python版本共存条件下,解决pip不能安装库的问题

我是使用的Ubuntu系统,系统自带了Python2.7和Python3.5。由于需要,我还在Ubuntu上安装了pycharm2018.1,在pycharm2018.1中创建了Python的虚拟的编译环境(pycharm自动创建的)。

问题描述:我打开终端输入

pip install requests
结果出现
Traceback (most recent call last):
  File "/usr/bin/pip", line 9, in <module>
    from pip import main
ImportError: cannot import name main

出现原因:因为我是多Python版本共存,而且还在pycharm中创建了虚拟环境。从而导致了系统自带的pip与pycharm虚拟环境中的pip冲突。

解决办法:

pip2 install requests

这里是指定将requests库安装在Python2下,如果用pip3则安装在Python3下(pip3需要使用apt命令安装,pip2是系统自带的pip命令,Ubuntu中系统默认使用Python2.7)。

sudo pip2 install requests
The directory '/home/zhiying/.cache/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
The directory '/home/zhiying/.cache/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag.
Collecting requests
  Downloading https://files.pythonhosted.org/packages/49/df/50aa1999ab9bde74656c2919d9c0c085fd2b3775fd3eca826012bef76d8c/requests-2.18.4-py2.py3-none-any.whl (88kB)
    100% |████████████████████████████████| 92kB 433kB/s 
Collecting certifi>=2017.4.17 (from requests)
  Downloading https://files.pythonhosted.org/packages/7c/e6/92ad559b7192d846975fc916b65f667c7b8c3a32bea7372340bfe9a15fa5/certifi-2018.4.16-py2.py3-none-any.whl (150kB)
    100% |████████████████████████████████| 153kB 962kB/s 
Collecting chardet<3.1.0,>=3.0.2 (from requests)
  Downloading https://files.pythonhosted.org/packages/bc/a9/01ffebfb562e4274b6487b4bb1ddec7ca55ec7510b22e4c51f14098443b8/chardet-3.0.4-py2.py3-none-any.whl (133kB)
    100% |████████████████████████████████| 143kB 3.1MB/s 
Collecting idna<2.7,>=2.5 (from requests)
  Downloading https://files.pythonhosted.org/packages/27/cc/6dd9a3869f15c2edfab863b992838277279ce92663d334df9ecf5106f5c6/idna-2.6-py2.py3-none-any.whl (56kB)
    100% |████████████████████████████████| 61kB 3.9MB/s 
Collecting urllib3<1.23,>=1.21.1 (from requests)
  Downloading https://files.pythonhosted.org/packages/63/cb/6965947c13a94236f6d4b8223e21beb4d576dc72e8130bd7880f600839b8/urllib3-1.22-py2.py3-none-any.whl (132kB)
    100% |████████████████████████████████| 133kB 4.1MB/s 
Installing collected packages: certifi, chardet, idna, urllib3, requests
  Found existing installation: chardet 2.3.0
    Not uninstalling chardet at /usr/lib/python2.7/dist-packages, outside environment /usr
Successfully installed certifi-2018.4.16 chardet-2.3.0 idna-2.6 requests-2.18.4 urllib3-1.22

注意第三库是安装在根目录下,因此需要加sudo命令。

如果要启动Python2 IDE只需要在终端输入python2就行:

zhiying@zhiying-Lenovo-XiaoXin-310-15IKB:~$ python2
Python 2.7.12 (default, Dec  4 2017, 14:50:18) 
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>>
输入python3便启动python3.5 IDE:
zhiying@zhiying-Lenovo-XiaoXin-310-15IKB:~$ python3
Python 3.5.2 (default, Nov 23 2017, 16:37:01) 
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 

下面检查requests库是否安装成功:

zhiying@zhiying-Lenovo-XiaoXin-310-15IKB:~$ python2
Python 2.7.12 (default, Dec  4 2017, 14:50:18) 
[GCC 5.4.0 20160609] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import requests
>>> 

没有报错,表明安装成功。

猜你喜欢

转载自blog.csdn.net/zhouzying/article/details/80542554
今日推荐