pip使用详解/pip换源

零、参考连接

https://pip.pypa.io/en/stable/user_guide
https://pip.pypa.io/en/stable/reference/pip_install/#usage
https://www.python.org/dev/peps/pep-0440/#version-specifiers

一、安装pip

  • Ubuntu:

    # python2
    sudo apt-get install python-pip
    # python3
    sudo apt-get install python3-pip
    
  • Windows:
    装好Python之后把Python安装目录下的Scripts文件夹的路径加入环境变量PATH就行。

  • 测试是否安装成功

    # python2
    pip -V
    # 或者
    pip2 -V
    # python3
    pip3 -V
    

    当pip命令无法运行时:

    # python2 and python3 on windows, python2 on linux
    python -m pip -V
    # python3 on linux
    python3 -m pip -V
    
  • 同时存在Python2和Python3时,在linux系统中,可以通过pip(或者pip2)和pip3分别为Python2和Python3安装库;而在Windows中Python2和Python3都有pip.exe,所以就需要使用pip2和pip3来分别为Python2和Python3安装库。为了节约篇幅,后面只使用pip作为例子讲解,需要为Python2安装库时,使用pip2命令;需要为Python3安装库时,使用pip3命令。

  • pip换源
    换源的就是把下载库的服务器切换为国内的镜像服务器,下载速度会快很多很多。

    • 清华的源
      Linux 打开终端,输入:
      mkdir  ~/.pip/
      echo [global] > ~/.pip/pip.conf
      echo index-url=https://pypi.tuna.tsinghua.edu.cn/simple >> ~/.pip/pip.conf
      
      Windows 打开命令行,输入:
      mkdir %USERPROFILE%\pip
      echo [global] > %USERPROFILE%\pip\pip.ini
      echo index-url=https://pypi.tuna.tsinghua.edu.cn/simple >> %USERPROFILE%\pip\pip.ini
      
      上面的命令就是创建 ~/.pip/pip.conf或者%USERPROFILE%\pip\pip.ini,然后在文件中写入:
      [global]
      index-url=https://pypi.tuna.tsinghua.edu.cn/simple
      
      %USERPROFILE%是用户目录; 是Ubuntu下的用户目录。
    • 阿里的源
      Linux 打开终端,输入:
      mkdir  ~/.pip/
      echo [global] > ~/.pip/pip.conf
      echo index-url=https://mirrors.aliyun.com/pypi/simple/  >> ~/.pip/pip.conf
      
      Windows 打开命令行,输入:
      mkdir %USERPROFILE%\pip
      echo [global] > %USERPROFILE%\pip\pip.ini
      echo index-url=https://mirrors.aliyun.com/pypi/simple/  >> %USERPROFILE%\pip\pip.ini
      
      上面的命令就是创建 ~/.pip/pip.conf或者%USERPROFILE%\pip\pip.ini,然后在文件中写入:
      [global]
      index-url=https://mirrors.aliyun.com/pypi/simple/ 
      

二、pip命令的基本使用方法

1、指定库的名字

  • 一次安装一个包
    pip install SomePackage             # 安装最新版本
    pip install SomePackage==1.0.4      # 安装指定版本1.0.4
    pip install 'SomePackage>=1.0.4'    # 最低版本是1.0.4
    pip install SomePackage.whl 		# 安装whl文件
    pip install http://wxpython.org/Phoenix/snapshot-builds/wxPython_Phoenix-3.0.3.dev1820+49a8884-cp34-none-win_amd64.whl  # 从链接安装
    pip install --upgrade --no-deps SomePackage # 升级SomePackage且不升级依赖
    pip install --user SomePackage 		# 把SomePackage安装到用户目录中
    
    在使用>=<等符号时,需要在两边加上单引号,应为><在命令行中是重定位输出和输入符。>=<等符号是版本限定的关系符。
  • 也可以一次安装多个包
    pip install Package Package2==1.0.4  'Package3>=1.0.4'          
    

2、从Requirements文件安装

pip install -r requirements.txt

requirements.txt的每一行是一个包。一个例子如下:

# 没有版本限定符
nose
beautifulsoup4

# 有版本限定符
# 版本限定符参考: https://www.python.org/dev/peps/pep-0440/#version-specifiers
docopt == 0.6.1             # 指定版本0.6.1 
keyring >= 4.1.1            # 最低版本 4.1.1
coverage != 3.5             # 除了版本3.5的任意版本都可以
Mopidy-Dirble ~= 1.1        # 兼容版, 等同于:>= 1.1, == 1.*

# 指定其他Requirements文件
-r other-requirements.txt

# 一个特定文件
./downloads/numpy-1.9.2-cp34-none-win32.whl
http://wxpython.org/Phoenix/snapshot-builds/wxPython_Phoenix-3.0.3.dev1820+49a8884-cp34-none-win_amd64.whl

更多关于Requirements的内容,请参考Requirements File Format

3、其他基本命令

  • 列出某个库所有的可用版本

    pip install numpy==
    

    ==符号后面没有版本号,这样pip就会报错并列出所有可用版本。

  • pip freeze: 以Requirements文件的格式列出当前Python环境的所有库

    absl-py==0.6.1
    asn1crypto==0.24.0
    astor==0.7.1
    backports-abc==0.5
    ...
    

    如果当前项目是使用的一个虚拟环境,要导出所有的依赖项时,就可以使用命令:

    pip freeze > requirements.txt
    

    在别处使用该项目时,就可以使用命令:

    pip install -r requirements.txt
    

    来安装本项目的所有依赖。

  • pip uninstall:卸载库

    pip uninstall SomePackage
    pip uninstall -y SomePackage 		# 不需要确认直接卸载
    pip uninstall --yes SomePackage 	# 同上
    pip uninstall -r requirements.txt 	# 卸载requirements.txt中的所有库
    
  • pip list: 列出库

    pip list 				# 列出所有库
    pip list --outdated  	# 列出可以更新的库
    pip list -o 			# 同上
    
  • pip show:显示已安装库的信息

    pip show numpy  	# 显示numpy的基本信息
    pip show numpy -f 	# 列出所有文件
    pip show numpy --file	# 同上
    
  • pip search:搜索库

    pip search "query"
    
  • pip download: 下载库

    # 把numpy下载以及numpy的依赖下载到DIR中
    pip download --destination-directory DIR numpy
    # 把requirements.txt中的所有库下载到DIR中
    pip download --destination-directory DIR -r requirements.txt 
    

    从本地下载好的文件安装:

    # 从DIR找到 requirements.txt中指定的库并安装
    pip install --no-index --find-links=DIR -r requirements.txt
    

三、控制库的版本-Version specifiers

Version specifiers, 我翻译为版本限定。版本限定的关系符包含:

    ~=: 兼容版本
    ==: 特定版本
    !=: 排除特定版本
    <=, >=: 小于等于\大于等于某个版本
    <, >: 小于\大于某个版本
    ===: 精确匹配

一些例子:

pip install numpy==1.15.4 	# 安装版本为1.15.4的numpy
pip install numpy!=1.15.4 	# 安装版本不为1.15.4的numpy,如果有更新版本,则会安装更新版本
pip install 'numpy<1.15.4'  # 安装小于版本小于1.15.4的numpy
pip install 'numpy<=1.15.4' # 安装小于版本小于等于1.15.4的numpy
pip install numpy==1.15.* 	# 安装版本为1.15开头的numpy
pip install numpy~=1.15.4   # 等同于>= 1.15.4, == 1.15.*

在使用包含<或者>符号时,在命令行中需要使用单引号括起来,否则<>会被命令行(终端)识别为重定向输入/输出符号。在Requirements文件中,不需要使用单引号括起来,这点和在终端中不同。
某些版本限定可以组合使用,中间使用英文逗号分割。例如:

pip install 'numpy>=1.9.*,< 1.16.3' # 安装版本大于1.9.*且版本小于1.16.3的numpy

在Requirements文件中,不需要单引号括起来,直接这样写就行:

numpy>=1.9.*,< 1.16.3

参考链接:https://www.python.org/dev/peps/pep-0440/#compatible-release

四、控制库的平台-Environment Markers

Environment Markers翻译为环境标记,实际就是除了满足版本限定符指定的版本之外,还要满足环境标记指定的条件,比如:

 pip install 'numpy==1.15.4;python_version<="3.6"'

就是除了numpy的版本要等于1.15.4之外,python_version的版本还要小于等于3.6。环境标记的关系符和版本限定的关系符相同。
环境标记的作用就是,假如开发一个兼容Python2和Python3的项目,但是依赖项在Python2和Python3的版本不同,就可以使用环境标记来完成。例如,某项目依赖于matplotlib,matplotlib 3.* 不支持Python2.*,只支持Python3.*,那么我们在写Requirements文件的时候,就可以像这样写:

matplotlib<=3.0.*; python_version<="2.7" 
matplotlib>=3.0.*; python_version>="3.5"

使用Python2时,就会安装2.*版本的matplotlib;使用Python3时,就会安装3.*版本的matplotlib。更多的环境标记如下表所示:

Marker Python equivalent Sample values
os_name os.name posix, java
sys_platform sys.platform linux, linux2, darwin, java1.8.0_51 (note that “linux” is from Python3 and “linux2” from Python2)
platform_machine platform.machine() x86_64
platform_python_implementation platform.python_implementation() CPython, Jython
platform_release platform.release() 3.14.1-x86_64-linode39, 14.5.0, 1.8.0_51
platform_system platform.system() Linux, Windows, Java
platform_version platform.version() #1 SMP Fri Apr 25 13:07:35 EDT 2014 Java HotSpot™ 64-Bit Server VM, 25.51-b03, Oracle Corporation Darwin Kernel Version 14.5.0: Wed Jul 29 02:18:53 PDT 2015; root:xnu-2782.40.9~2/RELEASE_X86_64
python_version platform.python_version()[:3] 3.4, 2.7
python_full_version platform.python_version() 3.4.0, 3.5.0b1
implementation_name sys.implementation.name cpython
implementation_version see definition below 3.4.0, 3.5.0b1
extra An error except when defined by the context interpreting the specification. test

参考链接:https://www.python.org/dev/peps/pep-0508/#environment-markers

发布了88 篇原创文章 · 获赞 132 · 访问量 18万+

猜你喜欢

转载自blog.csdn.net/DumpDoctorWang/article/details/89502120