1. 首先安装一个命令行下载工具,我这里使用axel:
$ axel https://bootstrap.pypa.io/get-pip.py
#下载完成之后得到一个python文件
$ ls
get-pip.py
也可以使用 wget:
wget https://bootstrap.pypa.io/get-pip.py
需要注意的是,当同时存在python2和python3时
$ ls -al /usr/bin/python*
lrwxrwxrwx 1 root root 7 Jul 20 03:53 python -> python3.6
lrwxrwxrwx 1 root root 9 Sep 20 01:26 python2 -> python2.7
#这里需要注意python命令代表的版本
# $ sudo python2 get-pip.py # python2 代表 python2.7
$ sudo python get-pip.py # python 代表 python3.6
Collecting pip
Downloading pip-9.0.1-py2.py3-none-any.whl (1.3MB)
100% |████████████████████████████████| 1.3MB 185kB/s
Collecting wheel
Downloading wheel-0.29.0-py2.py3-none-any.whl (66kB)
100% |████████████████████████████████| 71kB 492kB/s
Installing collected packages: pip, wheel
Successfully installed pip-9.0.1 wheel-0.29.0
# 查看安装结果:
$ sudo find / -name 'pip'
....
....
/usr/lib/python2.7/site-packages/pip
/usr/lib/python3.6/site-packages/pip
安装完成!
2. 安装模块的方法:
$ sudo pip install xxx //安装指定的 xxx 模块或者包
$ sudo pip uninstall xxx //删除指定的 xxx 模块或者包
$ pip list //查看当前的已安装python模块
使用pip安装模块时,需要注意python2和python3是否同时存:
$ ls -al /usr/bin/pip*
-rwxr-xr-x 1 root root 206 Oct 15 20:08 pip2
-rwxr-xr-x 1 root root 206 Oct 15 20:08 pip2.7
-rwxr-xr-x 1 root root 205 Sep 20 00:23 pip3
-rwxr-xr-x 1 root root 205 Sep 20 00:23 pip3.6
$ sudo pip3 install BeautifulSoup # 给 python3 安装模块这里用pip3
$ sudo pip2 install BeautifulSoup # 给 python2 安装模块这里用pip2
Collecting BeautifulSoup
Downloading BeautifulSoup-3.2.1.tar.gz
Building wheels for collected packages: BeautifulSoup
Running setup.py bdist_wheel for BeautifulSoup ... done
Stored in directory: /root/.cache/pip/wheels/5e/be/6d/ed01d5d434a821557b674c9da976f60b1b93d9009447eb9d16
Successfully built BeautifulSoup
Installing collected packages: BeautifulSoup
Successfully installed BeautifulSoup-3.2.1
# 查看已安装模块
# $ pip3 list python3
$ pip2 list
DEPRECATION: The default format will switch to columns in the future. You can use --format=(legacy|columns) (or define a format=(legacy|columns) in your pip.conf under the [list] section) to disable this warning.
BeautifulSoup (3.2.1)
netsnmp-python (1.0a1)
numpy (1.13.3)
pip (9.0.1)
pwquality (1.3.0)
setuptools (36.6.0)
team (1.0)
wheel (0.30.0)
另外当使用pip2或者pip3出现找不到python版本时,可以修改里面的环境 “#!/usr/bin/python2”
指向正确的python版本:
$ cd /usr/bin
$ ls -al python*
lrwxrwxrwx 1 root root 9 Sep 20 01:26 python2 -> python2.7
lrwxrwxrwx 1 root root 9 Oct 15 20:45 python3 -> python3.6
$ cat pip
#!/usr/bin/python2 # 这里指python2的pip,同理:#!/usr/bin/python3 指python3
# -*- coding: utf-8 -*-
import re
import sys
from pip import main
if __name__ == '__main__':
sys.argv[0] = re.sub(r'(-script\.pyw?|\.exe)?$', '', sys.argv[0])
sys.exit(main())
3. 在windows下使用pip的方法:
python2 和 python3 同时共存时:
python2:py -2 -m pip install path\file
python3:py -3 -m pip install path\file
#--------------------------------------------------
#例如在windows下安装wxpython: 两个步骤
1.安装wheel
#此为python2,安装python3则需更改 -3 即可
C:\>py -2 -m pip install wheel
2.安装wxpython的wheel文件:
#此为python2,安装python3则需更改 -3 即可
C:\>py -2 -m pip install D:\wxpython\windows\wxPython-4.0.0b2-cp36-cp36m-win_amd64.whl
4. 升级pip
sudo 后面的pip3为用户已安装的pip名称,比如python3版本的pip:pip3
$ sudo pip3 install --upgrade pip
Collecting pip
Downloading https://files.pythonhosted.org/packages/0f/74/ecd13431bcc456ed390b44c8a6e917c1820365cbebcb6a8974d1cd045ab4/pip-10.0.1-py2.py3-none-any.whl (1.3MB)
100% |████████████████████████████████| 1.3MB 215kB/s
Installing collected packages: pip
Found existing installation: pip 9.0.1
Not uninstalling pip at /usr/lib/python3/dist-packages, outside environment /usr
Successfully installed pip-10.0.1