pip 安装使用详解:http://www.ttlsa.com/python/how-to-install-and-use-pip-ttlsa python 包:https://pypi.python.org/pypi
在 python 中安装非自带 python 模块,有三种方式:
1. easy_install2. pip3. 下载压缩包(.zip, .tar, .tar.gz)后解压, 进入解压缩的目录后执行 python setup.py install 命令安装 setuptools,用 easy_install *.egg 命令来安装。一般 python setup.py install 安装后的模块,就能 import了 安装 python 模块,建议使用 pypi。
pip 命令 类似 RedHat 里面的 yum,安装 Python 包非常方便。
推荐:
python3 安装 pip 提示没有 distutils.util 模块错误的解决:https://www.cnblogs.com/phpper/p/10887602.html
curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py python get-pip.py pip -V // 查看 pip 版本以及 Python 版本 // pip 20.1.1 from /usr/local/lib/python2.7/dist-packages/pip (python 2.7) 如果安装的是 python3, 则 python3 默认安装的有 pip3 ,不需要再自己安装 pip3。 pip3 安装在 python3 路径下的 bin 目录里面 如果没有,则 执行 python3 get-pip.py pip3 -V // 查看 pip 版本以及 Python 版本 // pip 20.1.1 from /usr/local/lib/python3.8/dist-packages/pip (python 3.8)
升级 pip3
通常情况下,你的电脑里如果安装了python2.x也同时安装了python3.x,那么应该就会有两个pip。一个是pip2,另一个是pip3。好吧,可能还有一个既没有2,也米有3的pip,一般情况下,pip==pip2。
有时候我们使用pip安装东西会提示我们pip的版本过低,建议我们升级,一般情况下我们用pip3的时候固然会想到用下面的命令来升级:pip3 install --upgrade pip3。但如果你这么做了,你会发现好像这并不是正确的姿势。实际上这是一个逻辑错误,看了正确答案你就会知道哪里出错了。
正确的升级 pip3 如下:pip3 install --upgrade pip
清华大学开源软件镜像站:https://mirrors.tuna.tsinghua.edu.cn/help/pypi/
清华:https://pypi.tuna.tsinghua.edu.cn/simple 阿里云:http://mirrors.aliyun.com/pypi/simple/ 中国科技大学 https://pypi.mirrors.ustc.edu.cn/simple/ 华中理工大学:http://pypi.hustunique.com/ 山东理工大学:http://pypi.sdutlinux.org/ 豆瓣:http://pypi.douban.com/simple/
注意:新版ubuntu要求使用 https 源。
可以在使用 pip 的时候加参数 -i https://pypi.tuna.tsinghua.edu.cn/simple
例如:pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pyspider 这样就会从清华这边的镜像去安装 pyspider 库。
使用 豆瓣源 安装 robobrowser :pip install robobrowser -i http://pypi.douban.com/simple/
Linux下,修改 ~/.pip/pip.conf (没有就创建一个文件夹及文件。文件夹要加 “.”,表示是隐藏文件夹)
内容如下:
[global] index-url = https://pypi.tuna.tsinghua.edu.cn/simple [install] trusted-host=mirrors.aliyun.comwindows下,直接在user目录中创建一个pip目录,再新建文件pip.ini。(例如:C:\Users\WQP\pip\pip.ini)内容同上。
pip 下载
# wget "https://pypi.python.org/packages/source/p/pip/pip-1.5.4.tar.gz#md5=834b2904f92d46aaa333267fb1c922bb" --no-check-certificate或者 到 pypi 官网( https://pypi.python.org/pypi ) 直接下载源码安装
pip安装
# tar -xzvf pip-1.5.4.tar.gz # cd pip-1.5.4 # python setup.py install
pip 使用帮助
root@kali:~$ pip -h Usage: pip <command> [options] Commands: install 安装包. download 下载包. uninstall 卸载包. freeze 按照一定格式输出已安装包的列表. list 列出已经安装的包. show 显示所安装包的信息. check 验证已安装的包所具有的兼容依赖关系. search 通过 PyPI 搜索包. wheel Build wheels from your requirements. hash 计算包文件的哈希值. completion 用于命令完成的助手命令。 help 命令帮助. General Options: -h, --help 显示帮助. --isolated Run pip in an isolated mode, ignoring environment variables and user configuration. -v, --verbose 更多的输出,最多可以使用3次 -V, --version 显示版本号并退出 -q, --quiet 安静模式,显示最少的输出。 --log <path> Path to a verbose appending log. --proxy <proxy> 使用代理。代理格式: [user:passwd@]proxy.server:port. --retries <retries> Maximum number of retries each connection should attempt (default 5 times). --timeout <sec> 设置 socket 超时时间。(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 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.如果想看 pip 中某个 命令的帮助,例如 pip list ,可以使用: pip list -h
root@kali:~$ pip list -h Usage: pip list [options] Description: List installed packages, including editables. Packages are listed in a case-insensitive sorted order. List Options: -o, --outdated List outdated packages -u, --uptodate List uptodate packages -e, --editable List editable projects. -l, --local If in a virtualenv that has global access, do not list globally-installed packages. --user Only output packages installed in user-site. --pre Include pre-release and development versions. By default, pip only finds stable versions. --format <list_format> Select the output format among: legacy (default), columns, freeze or json. --not-required List packages that are not dependencies of installed packages. Package Index Options (including deprecated options): -i, --index-url <url> Base URL of Python Package Index (default https://pypi.python.org/simple). This should point to a repository compliant with PEP 503 (the simple repository API) or a local directory laid out in the same format. --extra-index-url <url> Extra URLs of package indexes to use in addition to --index-url. Should follow the same rules as --index-url. --no-index Ignore package index (only looking at --find-links URLs instead). -f, --find-links <url> If a url or path to an html file, then parse for links to archives. If a local path or file:// url that's a directory, then look for archives in the directory listing. --process-dependency-links Enable the processing of dependency links.示例: pip list --outdata 查看需要更新的 python 包
使用 pip 安装包
# pip install SomePackage [...] Successfully installed SomePackage
使用 pip 查看已安装的包
# pip show --files SomePackage Name:SomePackage Version:1.0 Location:/my/env/lib/pythonx.x/site-packages Files: ../somepackage/__init__.py [...]
使用 pip 检查哪些包需要更新
# pip list --outdated SomePackage (Current: 1.0 Latest: 2.0)
使用 pip 升级包
# pip install --upgrade SomePackage [...] Foundexistinginstallation:SomePackage1.0 UninstallingSomePackage: SuccessfullyuninstalledSomePackage Runningsetup.pyinstallforSomePackage SuccessfullyinstalledSomePackage
使用 pip 卸载包
$ pip uninstall SomePackage Uninstalling SomePackage: /my/env/lib/pythonx.x/site-packages/somepackage Proceed (y/n)? y Successfully uninstalled SomePackage
以 安装 redis 示例:
安装redis # pip install redis 卸载redis # pip uninstall redis Uninstalling redis: /usr/lib/python2.6/site-packages/redis-2.9.1-py2.6.egg-info .....省略一些内容.... Proceed (y/n)? y Successfully uninstalled redis 查看待更新包 pip list --outdate
常见错误:ImportError No module named setuptools。说明没有安装 setuptools 模块,直接下载安装即可
