pip工具使用指南

239 阅读4分钟

pip使用指南

pip官网

pip使用指南pipLinux安装pip工具Windows安装pippip的基本使用更新pip工具pip helppip 临时更换安装源

pip

pip是python包管理工具,该工具提供了对python包的查找、下载、安装和卸载等功能。

python官网下载的安装包已经携带了pip工具。python2.7.9或者python3.4以上版本都自带了pip工具。

记得要在官网下载python安装包。

如果没有pip工具(或者卸载了)的,要另外安装,在Linux下面安装很简单,Windows就有点麻烦了,所以没事就别卸载了。

Linux安装pip工具

$ curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py   # 下载安装脚本
$ sudo python get-pip.py    # 运行安装脚本</pre>

这里需要注意的是,如果操作系统中有多个python解释器,,哪个python解释器运行安装脚本,pip就关联到哪个python解释器。

假设当前Linux中有3个python的解释器,python2.7、python3.6两个 python3.6安装pip则需要运行python3的解释器执行脚本 sudo python3 get-pip.py即可 一般pip对应的是python2.7,pip3对应的是python3,系统中含有两个版本的python的情况下。

部分Linux发行版可直接用包管理器安装pip,如Debian和Ubuntu:

sudo apt-get install python-pip

Windows安装pip

Windows安装就比较麻烦了。

首先需要下载pip,下载地址pip下载地址,注意,这里要选择下载后缀为tar.gz的压缩包。 选择pip下载

pip下载

下载完成之后,解压缩。进去该文件夹,setup.py文件同级的目录。

1582382698108.png

在这个目录打开cmd或者power shell(或者打开cmd然后进入这个目录都可以),然后执行以下命令安装pip

python setup.py install

这里也是一样,如果有多个python版本,则使用需要安装pip的python执行脚本即可。

接下来就是添加环境变量了

添加windows系统环境变量,与安装python时添加的方法一样 如我的python目录是:D:\Python27; 则添加如下2个目录到系统环境变量里:D:\Python27;D:\Python27\Scripts;

记住,是系统环境变量

添加完环境变量后测试一下是否安装成功,打开cmd输入以下命令

pip --version

可以看到终端打印出pip的版本及路径即可。 显示pip的安装版本以及路径

pip的基本使用

pip工具需要在命令行使用。

pip --version  # 获取pip版本及路径
pip --help  # 获取pip帮助
​
# 库管理相关
pip list  # 列出已安装的所有python包的信息
pip show  # 列出已安装库的详细信息
pip search SomePackage  # 通过pypi搜索库
pip show -f somepackage  # 查看指定包的详细信息</pre>

库的安装卸载

pip install packagename  # 安装库,通过网络在pypi中搜索下载指定库进行安装

pip install SomePackage              # 最新版本
pip install SomePackage==1.0.4       # 指定版本,指定版本时,是两个等号'=='
pip install 'SomePackage>=1.0.4'     # 最小版本</pre>

升级包

pip install --upgrade SomePackage  # 升级包

pip install -U somepackage  # 安装包,如果已安装,则升级到最新版本

如果是直接使用pip install somepackage安装包,如果包已安装,不管是否最新版本,都会只提示该包已安装,不会进行升级。

卸载包

pip uninstall somepackage  # 卸载包

查看可以升级的包

pip list --outdated  # 显示所有已安装的有更新的包

更新pip工具

在使用pip工具的时候,若操作系统连接了网络,则pip会自动检查是否有新版本,若检查到新版本,则会提示

WARNING: You are using pip version 19.3.1; however, version 20.0.2 is available.
You should consider upgrading via the 'python -m pip install --upgrade pip' command.
​
# 这时候只需要按照提示进行输入命令升级即可
python -m pip install --upgrade pip</pre>

然后就会自动下载安装更新了,如果网络不好则可能会下载失败,多试几次就好了。

pip help

pip --help
​
Usage:
 pip <command> [options]
​
Commands:
 install                     Install packages.
 download                    Download packages.
 uninstall                   Uninstall packages.
 freeze                      Output installed packages in requirements format.       list                        List installed packages.
 show                        Show information about installed packages.
 check                       Verify installed packages have compatible dependencies.
 config                      Manage local and global configuration.
 search                      Search PyPI for packages.
 wheel                       Build wheels from your requirements.
 hash                        Compute hashes of package archives.
 completion                  A helper command used for command completion.
 debug                       Show information useful for debugging.
 help                        Show help for commands.
​
General Options:
 -h, --help                  Show help.
 --isolated                  Run pip in an isolated mode, ignoring environment                                   variables and user configuration.
 -v, --verbose               Give more output. Option is additive, and can be                                    used up to 3 times.
 -V, --version               Show version and exit.
 -q, --quiet                 Give less output. Option is additive, and can be                                    used up to 3 times (corresponding to WARNING, ERROR,                                and CRITICAL logging levels).
 --log <path>                Path to a verbose appending log.
 --proxy <proxy>             Specify a proxy in the form
 [user:passwd@]proxy.server:port.
 --retries <retries>         Maximum number of retries each connection should                                    attempt (default 5 times).
 --timeout <sec>             Set the socket timeout (default 15 seconds).
 --exists-action <action>    Default action when a path already exists: (s)witch,                                (i)gnore, (w)ipe, (b)ackup, (a)bort.
 --trusted-host <hostname>   Mark this host or host:port pair as trusted, even                                   though it does not have valid or any HTTPS.
 --cert <path>               Path to alternate CA bundle.
 --client-cert <path>        Path to SSL client certificate, a single file
 containing the private key and the certificate in                                   PEM format.
 --cache-dir <dir>           Store the cache data in <dir>.
 --no-cache-dir              Disable the cache.
 --disable-pip-version-check
 Don't periodically check PyPI to determine whether a                                new version of pip is available for download.
 Implied with --no-index.
 --no-color                  Suppress colored output

pip 临时更换安装源

  • Windows上操作

    • 更换豆瓣源

      • shellpip install  -i https://pypi.doubanio.com/simple/  --trusted-host pypi.doubanio.com wordcloud

      • wordcloud为目标库,https://pypi.doubanio.com/simple/为豆瓣源地址,-i命令即选择临时源安装第三方库