Python提供了大量的第三方库来给咱们使用,这也是Python的强大之处
pip是Python的包管理工具,可以很方便的安装和管理第三方库(包)。类似于PHP中Composer,或者Nodejs中的npm,以及Linux中的yum
可以直接使用 pip install 包名
来安装指定三方库
例如安装 requests 库:
pip install requests
可以通过 pip --help
或pip -h
查看 pip 的帮助文档
>> 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.
cache Inspect and manage pip's wheel cache.
index Inspect information available from package indexes.
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.
--debug Let unhandled exceptions propagate outside the
main subroutine, instead of logging them to
stderr.
--isolated Run pip in an isolated mode, ignoring
environment variables and user configuration.
--require-virtualenv Allow pip to only run in a virtual environment;
exit with an error otherwise.
-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 to a verbose appending log.
--no-input Disable prompting for input.
--proxy Specify a proxy in the form
[user:passwd@]proxy.server:port.
--retries Maximum number of retries each connection should
attempt (default 5 times).
--timeout Set the socket timeout (default 15 seconds).
--exists-action Default action when a path already exists:
(s)witch, (i)gnore, (w)ipe, (b)ackup, (a)bort.
--trusted-host Mark this host or host:port pair as trusted,
even though it does not have valid or any HTTPS.
--cert Path to PEM-encoded CA certificate bundle. If
provided, overrides the default. See ' SSL
Certificate Verification' in pip documentation
for more information.
--client-cert Path to SSL client certificate, a single file
containing the private key and the certificate
in PEM format.
--cache-dir Store the cache data in .
--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.
--no-python-version-warning
Silence deprecation warnings for upcoming
unsupported Pythons.
--use-feature <feature> Enable new functionality, that may be backward
incompatible.
--use-deprecated <feature> Enable deprecated functionality, that will be
removed in the future.
pip install -U pip
pip list
pip show
# 展示包文件所在路径
pip show -f <包名>
pip install [options] <requirement specifier> [package-index-options] ...
pip install [options] -r <requirements file> [package-index-options] ...
pip install [options] [-e] <vcs project url> ...
pip install [options] [-e] <local project path> ...
pip install [options] <archive url/path> ...
# 栗子
pip install requests
pip install -r requirements.txt
pip install package # 最新版本
pip install package==1.0.4 # 指定版本
pip install 'package>=1.0.4' # 最小版本
# 在当前目录下生成requirements.txt
pip freeze > requirements.txt
使用 -i
参数即可:
pip install -i https://pypi.douban.com/simple/ <包名>
清华: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/
修改 ~/.pip/pip.conf
没有就创建一个,内容如下:
[global]
timeout = 6000 #指定超时时间 ms
index-url = https://pypi.tuna.tsinghua.edu.cn/simple #源地址
trusted-host = pypi.tuna.tsinghua.edu.cn #信任域名
直接在user目录中创建一个pip目录,如:C:\Users\xx\pip,在pip 目录下新建文件pip.ini,内容如下:
或者按照网友的建议:win+R 打开用户目录%HOMEPATH%,在此目录下创建 pip 文件夹,在 pip 目录下创建 pip.ini 文件, 内容如下:
[global]
timeout = 6000
index-url = https://pypi.tuna.tsinghua.edu.cn/simple
trusted-host = pypi.tuna.tsinghua.edu.cn
pip install <包名> -d <目录>
pip install -d <目录> -r requirements.txt
pip install --upgrade <包名>
pip install -U <包名> # --upgrade 可简写为 -U
pip uinstall <包名> #卸载指定包
pip uinstall -r requirement.txt #卸载requirement内的指定的包
pip list --outdated # 列出所有过期的库
pip list -o # --outdated的简写,列出所有过期的库
python -m pip uninstall pip
https://www.cnblogs.com/poloyy/p/15170969.html
我们在使用python第三方库的时候,经常是用到什么库就去pip install ,需要哪一个下载哪一个。但是当我们需要更新第三方库的时候,手动的一个个的去更新的话,恐怕是要耗费很多时间,下面就介绍一下怎么批量更新Python的三方包,这里只介绍两种常用(推荐)的方式:
# 安装pip-review
> pip install pip-review
# 检查是否有需要更新的包
> pip-review
Everything up-to-date
# 自动更新所有包 --auto -a
> pip-review -a
# 手动为每个包选择是否更新 --interactive -i
> pip-review --interactive
matplotlib==3.3.3 is available (you have 3.1.3)
Upgrade now? [Y]es, [N]o, [A]ll, [Q]uit N
numpy==1.19.4 is available (you have 1.18.1)
Upgrade now? [Y]es, [N]o, [A]ll, [Q]uit Y
pandas==1.1.5 is available (you have 1.0.3)
Upgrade now? [Y]es, [N]o, [A]ll, [Q]uit N
...
# 安装pipupgrade
> pip install pipupgrade
# 批量更新
# -V --verbose:显示详细输出
> pipupgrade -V -l -y
# -s --self:更新 pipupgrade
> pipupgrade --self
# -c --check:检查并打印过时的软件包(不执行升级)
> pipupgrade --check
# -l --latest:升级所有包 ,包括破坏更改的包
> pipupgrade --latest
# -a --all:列出所有包
> pipupgrade --all
# -i --interactive:更新每个包都显示确认框
> pipupgrade --interactive
# -r --requirements:根据 requiremenet.txt 文件升级
> pipupgrade --requirement requirement.txt
# -y、--yes:一次性确认所有包更新的确认框
> pipupgrade -y
如果你现在用Django 1.10.x写了个网站,然后你的领导跟你说,之前有一个旧项目是用Django 0.9开发的,让你来维护,但是Django 1.10不再兼容Django 0.9的一些语法了。这时候就会碰到一个问题,我如何在我的电脑中同时拥有Django 1.10和Django 0.9两套环境呢?这时候我们就可以通过虚拟环境来解决这个问题
主要有两种方式创建:
会生成一个venv的目录:
打开venv目录:
![](https://img-blog.csdnimg.cn/img_convert/b06ca2d5f3f24962cc883fbd8052d40e.webp?x-oss-process=image/format,png#clientId=u8218a864-5691-4&crop=0&crop=0&crop=1&crop=1&from=paste&id=u3dea9f9d&margin=[object Object]&originHeight=471&originWidth=772&originalType=url&ratio=1&rotation=0&showTitle=false&status=done&style=none&taskId=ue1965a11-343d-44a5-8da8-76343911178&title=)
特别注意:pycharm新建的虚拟环境需要重启ide才会生效
![](https://img-blog.csdnimg.cn/img_convert/06a67460152e86aa8c01559df5c53b88.webp?x-oss-process=image/format,png#clientId=u8218a864-5691-4&crop=0&crop=0&crop=1&crop=1&from=paste&id=uaa2fa7b5&margin=[object Object]&originHeight=364&originWidth=643&originalType=url&ratio=1&rotation=0&showTitle=false&status=done&style=none&taskId=u743c14d6-8e7b-4454-9406-af74e7eb414&title=)
cd 到venv/Scripts 输出:deactivate 退出虚拟环境
![](https://img-blog.csdnimg.cn/img_convert/850bb3911ba687f87f88cdb737cc9082.webp?x-oss-process=image/format,png#clientId=u8218a864-5691-4&crop=0&crop=0&crop=1&crop=1&from=paste&id=u65830b53&margin=[object Object]&originHeight=291&originWidth=875&originalType=url&ratio=1&rotation=0&showTitle=false&status=done&style=none&taskId=u0cc6c058-8e87-4de7-a743-0e85a11905b&title=)
activate 重新激活虚拟环境
pip install virtualenv
virtualenv <虚拟环境的名字>
虚拟环境创建好了以后,那么可以进入到这个虚拟环境中,然后安装一些第三方包,进入虚拟环境在不同的操作系统中有不同的方式,一般分为两种:Windows和Linux
# 激活:进入到虚拟环境的Scripts文件夹中,然后执行activate命令
activate
# 退出虚拟环境
deactivate
# 激活虚拟环境
source /path/to/virtualenv/bin/activate
# 退出虚拟环境
deactivate
要删除虚拟环境,同样删除虚拟环境目录即可
-p
参数来指定具体的Python解释器virtualenv -p C:\Python39\python.exe [virutalenv name]
主流的虚拟环境和包管理工具大致如下:
Poetry使用:https://www.yuque.com/li-sheng/study/usg7vh