问题描述:
在Ubuntu16.04中,我一直是用pip来安装Python所需的包的,今天我用Python3时,报错:提示说包不存在!我在想,这是为什么呢?我不是已经用pip安装过包了吗,为什么在Python3中提示没有安装包?
探索过程:
Ubuntu16.04自带的Python两个版本:
我在ipython3中导入numpy 包时报错:
既然提示我没有这个包,那么我就安装这个包
提示说这个包已经安装了,我切换到Python3的这个文件夹下面的dist-packages目录下,发现下面时空的;这时我产生一个想法,既然都是Python的包,那么我能不能把Python2.7安装的包,即Python2.7下的dist-packages这个文件夹直接复制到Python3中,这样不就可以用了吗,于是我这样做了
复制完后,我再次在ipython3中导入numpy这个包
发现再次报错!
查找资料发现,Python2.7安装包时是用pip安装的,然而,Python3安装包时,需要用pip3来安装
分别显示各自的pip list和pip3 list
Ubuntu默认Python为2.7,所以安装Python包时安装的为py2的包。
在py2下执行pip install xxx命令安装的是py2对应的安装包,系统会自动选择下载。
所以要是想在Python3中用包,需要用pip3 install xxx,来安装Python3的包。这样问题就得到了解决。
I ran into this issue myself recently and found that I wasn't getting the right pip for python 3, on my Linux system that also has Python 2.
First you must ensure that you have installed pip for your python version:
For Python 2:
sudo apt-get install python-pip
For Python 3:
sudo apt-get install python3-pip
Then to install packages for one version of Python or the other, simply use the following for Python 2:
pip install <package>
or for Python 3:
pip3 install <package>
pip is also a python package. So the easiest way to install modules to a specific python version would be below
python2.7 /usr/bin/pip install foo
or
python2.7 -m pip install foo