相信对于大多数熟悉Python的人来说,一定都听说并且使用过pip这个工具,但是对它的了解可能还不一定是非常的透彻。
当然在Python 3.4版本之后以及Python 2.7.9版本之后,官网的安装包当中就已经自带了pip,用户直接在安装完Python之后就可以直接使用,要是使用由virtualenv或者pyvenv创建的虚拟环境,那么pip也是被默认安装的。
如果是需要自己另外安装pip包的,在已经配置好Python的环境当中运行下面这个命令行:
py -m ensurepip --upgrade
另外一种方式是从官网上(https://bootstrap.pypa.io/get-pip.py
)直接下载get-pip.py脚本,然后直接运行python get-pip.py脚本即可。
安装后,在命令行中输入pip,然后按下回车,就会出现下图所示的使用说明:
Usage:
pip <command> [options]
Commands:
install Install packages.
download Download packages.
uninstall Uninstall packages.
freeze Output installed packages in requirements format.
inspect Inspect the python environment.
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> Path to a verbose appending log.
--no-input Disable prompting for input.
--proxy <proxy> Specify a proxy in the form scheme://[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 PEM-encoded CA certificate bundle. If provided, overrides the default. See
'SSL Certificate Verification' in pip documentation for more information.
--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.
--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版本有点低,想要升级一下的话,在命令行中输入以下命令:
pip install --upgrade pip
或者是:
pip install -U pip
如果打算用pip来安装第三方的包,用的是以下的命令行:
pip install package-name
例如我们想要安装指定版本的第三方的包,例如安装3.4.1版本的matplotlib:
pip install matplotlib==3.4.1
要是你打算想要卸载某个包,该要输入的命令行是:
pip uninstall package_name
而如果打算更新某个包,对应的命令行是:
pip install --upgrade package_name
# 或者是
pip install -U package_name
可以通过以下的这个命令行来查看指定包的信息:
pip show -f requests
Usage:
pip <command> [options]
Commands:
install Install packages.
download Download packages.
uninstall Uninstall packages.
freeze Output installed packages in requirements format.
inspect Inspect the python environment.
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> Path to a verbose appending log.
--no-input Disable prompting for input.
--proxy <proxy> Specify a proxy in the form scheme://[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 PEM-encoded CA certificate bundle. If provided, overrides the default. See
'SSL Certificate Verification' in pip documentation for more information.
--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.
--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.
(PyTorch) D:\CodeProject>pip show -f requests
Name: requests
Version: 2.31.0
Summary: Python HTTP for Humans.
Home-page: https://requests.readthedocs.io
Author: Kenneth Reitz
Author-email: me@kennethreitz.org
License: Apache 2.0
Location: d:\anaconda\envs\pytorch\lib\site-packages
Requires: certifi, charset-normalizer, idna, urllib3
Required-by: basicsr, datasets, diffusers, docker, duckduckgo-search, folium, gdown, google-api-core, gTTS, huggingface-hub, msn, openai, pinecone-client, pooch, requests-oauthlib, responses, spacy, tb-nightly, tensorboard, tiktoken, torchvision, transformers, ultralytics, uszipcode, webdriver-manager
Files:
requests-2.31.0.dist-info\INSTALLER
requests-2.31.0.dist-info\LICENSE
requests-2.31.0.dist-info\METADATA
requests-2.31.0.dist-info\RECORD
requests-2.31.0.dist-info\REQUESTED
requests-2.31.0.dist-info\WHEEL
requests-2.31.0.dist-info\top_level.txt
requests\__init__.py
requests\__pycache__\__init__.cpython-310.pyc
requests\__pycache__\__version__.cpython-310.pyc
requests\__pycache__\_internal_utils.cpython-310.pyc
requests\__pycache__\adapters.cpython-310.pyc
requests\__pycache__\api.cpython-310.pyc
requests\__pycache__\auth.cpython-310.pyc
requests\__pycache__\certs.cpython-310.pyc
requests\__pycache__\compat.cpython-310.pyc
requests\__pycache__\cookies.cpython-310.pyc
requests\__pycache__\exceptions.cpython-310.pyc
requests\__pycache__\help.cpython-310.pyc
requests\__pycache__\hooks.cpython-310.pyc
requests\__pycache__\models.cpython-310.pyc
requests\__pycache__\packages.cpython-310.pyc
requests\__pycache__\sessions.cpython-310.pyc
requests\__pycache__\status_codes.cpython-310.pyc
requests\__pycache__\structures.cpython-310.pyc
requests\__pycache__\utils.cpython-310.pyc
requests\__version__.py
requests\_internal_utils.py
requests\adapters.py
requests\api.py
requests\auth.py
requests\certs.py
requests\compat.py
requests\cookies.py
requests\exceptions.py
requests\help.py
requests\hooks.py
requests\models.py
requests\packages.py
requests\sessions.py
requests\status_codes.py
requests\structures.py
requests\utils.py
我们需要查看一下现有的这些包中,哪些是需要是被升级的,可以用下面这行命令行来查看:
pip list -o
Package Version Latest Type
------------------------ ----------- ----------- -----
absl-py 1.3.0 1.4.0 wheel
accelerate 0.18.0 0.20.3 wheel
antlr4-python3-runtime 4.9.3 4.13.0 wheel
anyio 3.6.2 3.7.0 wheel
asttokens 2.1.0 2.2.1 wheel
attrs 22.1.0 23.1.0 wheel
auto_gpt_plugin_template 0.0.2 0.0.3 wheel
black 21.4b2 23.3.0 wheel
bleach 5.0.1 6.0.0 wheel
blis 0.7.9 0.9.1 wheel
Bottleneck 1.3.5 1.3.7 wheel
cachetools 5.2.0 5.3.1 wheel
category-encoders 2.5.1.post0 2.6.1 wheel
certifi 2022.12.7 2023.5.7 wheel
cloudpickle 2.2.0 2.2.1 wheel
contourpy 1.0.6 1.1.0 wheel
cryptography 38.0.1 41.0.1 wheel
Cython 0.29.34 0.29.35 wheel
d2l 0.15.1 0.17.6 wheel
datasets 2.12.0 2.13.0 wheel
debugpy 1.6.3 1.6.7 wheel
diffusers 0.16.1 0.17.1 wheel
duckduckgo-search 3.0.2 3.8.3 wheel
fastjsonschema 2.16.2 2.17.1 wheel
Fiona 1.9.3 1.9.4.post1 wheel
flatbuffers 23.3.3 23.5.26 wheel
flit_core 3.8.0 3.9.0 wheel
fonttools 4.38.0 4.40.0 wheel
fsspec 2023.4.0 2023.6.0 wheel
ftfy 6.0.3 6.1.1 wheel
gast 0.4.0 0.5.4 wheel
geopandas 0.13.0 0.13.2 wheel
gfpgan 1.3.5 1.3.8 wheel
google-auth 2.17.3 2.20.0 wheel
grpcio 1.50.0 1.54.2 wheel
gTTS 2.3.1 2.3.2 wheel
h5py 3.7.0 3.9.0 wheel
hijri-converter 2.3.0.post1 2.3.1 wheel
holidays 0.23 0.27 wheel
huggingface-hub 0.13.3 0.15.1 wheel
imageio 2.23.0 2.31.1 wheel
imbalanced-learn 0.10.0 0.10.1 wheel
importlib-metadata 5.0.0 6.7.0 wheel
iopath 0.1.9 0.1.10 sdist
ipykernel 6.17.1 6.23.2 wheel
ipython 8.6.0 8.14.0 wheel
ipywidgets 8.0.2 8.0.6 wheel
jax 0.4.8 0.4.12 sdist
jedi 0.18.1 0.18.2 wheel
jsonschema 4.17.0 4.17.3 wheel
jupyter_client 7.4.4 8.2.0 wheel
jupyter-console 6.4.4 6.6.3 wheel
jupyter_core 5.0.0 5.3.1 wheel
jupyter-server 1.23.1 2.6.0 wheel
jupyterlab-widgets 3.0.3 3.0.7 wheel
Levenshtein 0.21.0 0.21.1 wheel
lightgbm 3.3.3 3.3.5 wheel
llvmlite 0.39.1 0.40.0 wheel
Markdown 3.3.7 3.4.3 wheel
MarkupSafe 2.1.1 2.1.3 wheel
matplotlib 3.6.2 3.7.1 wheel
missingno 0.5.1 0.5.2 wheel
mistune 2.0.4 3.0.1 wheel
ml-dtypes 0.1.0 0.2.0 wheel
mlxtend 0.21.0 0.22.0 wheel
munch 2.5.0 3.0.0 wheel
nbclassic 0.4.8 1.0.0 wheel
nbclient 0.7.0 0.8.0 wheel
nbconvert 7.2.4 7.6.0 wheel
nbformat 5.7.0 5.9.0 wheel
networkx 2.8.8 3.1 wheel
notebook 6.5.2 6.5.4 wheel
notebook_shim 0.2.2 0.2.3 wheel
numba 0.56.4 0.57.0 wheel
numexpr 2.8.3 2.8.4 wheel
numpy 1.23.3 1.25.0 wheel
onnx 1.13.1 1.14.0 wheel
onnxruntime 1.14.1 1.15.1 wheel
open-clip-torch 2.7.0 2.20.0 wheel
openai 0.27.2 0.27.8 wheel
openapi-python-client 0.13.4 0.14.1 wheel
opencv-python 4.7.0.68 4.7.0.72 wheel
openpyxl 3.0.10 3.1.2 wheel
orjson 3.8.10 3.9.1 wheel
packaging 21.3 23.1 wheel
pandas 1.4.4 2.0.2 wheel
Pillow 9.2.0 9.5.0 wheel
pinecone-client 2.2.1 2.2.2 wheel
pip 22.2.2 23.1.2 wheel
playsound 1.2.2 1.3.0 sdist
plotly 5.14.0 5.15.0 wheel
pooch 1.6.0 1.7.0 wheel
prettytable 3.7.0 3.8.0 wheel
prometheus-client 0.15.0 0.17.0 wheel
psutil 5.9.4 5.9.5 wheel
pyaml 21.10.1 23.5.9 wheel
pyarrow 11.0.0 12.0.1 wheel
pyasn1 0.4.8 0.5.0 wheel
pyasn1-modules 0.2.8 0.3.0 wheel
pygame 2.1.2 2.4.0 wheel
Pygments 2.13.0 2.15.1 wheel
PyMuPDF 1.22.2 1.22.3 wheel
pyOpenSSL 22.0.0 23.2.0 wheel
pyparsing 3.0.9 3.1.0 wheel
pypinyin 0.47.1 0.49.0 wheel
pyproj 3.5.0 3.6.0 wheel
pyrsistent 0.19.2 0.19.3 wheel
pytest 7.3.1 7.3.2 wheel
python-Levenshtein 0.21.0 0.21.1 wheel
pytools 2022.1.14 2023.1 wheel
pytorch-lightning 2.0.2 2.0.3 wheel
pytz 2022.1 2023.3 wheel
pywin32 305 306 wheel
pywinpty 2.0.9 2.0.10 wheel
pyzmq 24.0.1 25.1.0 wheel
qtconsole 5.4.0 5.4.3 wheel
QtPy 2.3.0 2.3.1 wheel
rapidfuzz 3.0.0 3.1.1 wheel
regex 2023.3.23 2023.6.3 wheel
responses 0.18.0 0.23.1 wheel
scikit-image 0.19.3 0.21.0 wheel
scikit-learn 1.2.0 1.2.2 wheel
selenium 4.1.4 4.10.0 wheel
Send2Trash 1.8.0 1.8.2 wheel
sentry-sdk 1.15.0 1.25.1 wheel
setuptools 65.5.0 68.0.0 wheel
soupsieve 2.3.2.post1 2.4.1 wheel
SQLAlchemy 1.4.47 2.0.16 wheel
stack-data 0.6.0 0.6.2 wheel
statsmodels 0.13.5 0.14.0 wheel
supervision 0.6.0 0.10.0 wheel
sympy 1.11.1 1.12 wheel
tensorboard 2.12.2 2.13.0 wheel
tensorboard-data-server 0.7.0 0.7.1 wheel
tensorboardX 2.4.1 2.6.1 wheel
termcolor 2.2.0 2.3.0 wheel
terminado 0.17.0 0.17.1 wheel
tifffile 2022.10.10 2023.4.12 wheel
tiktoken 0.3.3 0.4.0 wheel
timm 0.6.13 0.9.2 wheel
torch 1.12.1 2.0.1 wheel
torchaudio 0.12.1 2.0.2 wheel
torchvision 0.13.1 0.15.2 wheel
tornado 6.2 6.3.2 wheel
traitlets 5.5.0 5.9.0 wheel
transformers 4.28.1 4.30.2 wheel
trio-websocket 0.10.2 0.10.3 wheel
typeguard 2.13.3 4.0.0 wheel
typer 0.7.0 0.9.0 wheel
typing_extensions 4.4.0 4.6.3 wheel
ultralytics 8.0.35 8.0.120 wheel
urllib3 1.26.12 2.0.3 wheel
vcrpy 4.2.1 4.3.1 wheel
wcwidth 0.2.5 0.2.6 wheel
websocket-client 1.4.2 1.6.0 wheel
Werkzeug 2.2.2 2.3.6 wheel
wheel 0.38.4 0.40.0 wheel
widgetsnbextension 4.0.3 4.0.7 wheel
wordcloud 1.8.2.2 1.9.2 wheel
wrapt 1.14.1 1.15.0 wheel
xgboost 1.7.2 1.7.6 wheel
yapf 0.33.0 0.40.1 wheel
zipp 3.10.0 3.15.0 wheel
在下载安装一些标准库的时候,需要考虑到兼容问题,一些标准库的安装可能需要依赖其他的标准库,会存在版本相冲突等问题,我们先用下面这条命令行来检查一下是否会有冲突的问题存在:
pip check package_name
当然要是我们不指定是哪个标准库的话,会检查现在已经安装的所有包中的是否存在版本冲突等问题:
pip check
输出结果:
yfinance 0.1.70 has requirement requests>=2.26, but you have requests 2.24.0.
selenium 4.1.0 has requirement urllib3[secure]~=1.26, but you have urllib3 1.25.11.
我们要是感觉到安装的速度有点慢,可以指定国内的源来安装某个包,例如:
pip install -i https://pypi.douban.com/simple/ package_name
国内源有:
清华: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 download package_name -d "某个路径"
例如:
pip download requests -d "."
就是在当前的目录下下载requests模块以及其他所要依赖的模块。
我们一般在看到别人的项目时,都会包含一个requirements.txt文件,里面包含了一些Python项目当中需要用到的第三方库:
要生成这种txt文件,需要这么来做:
pip freeze > requirements.txt
而如果我们需要来批量安装第三方库,在命令行中输入以下这个命令:
pip install -r requirements.txt
下载源,即我们下载东西的网址;由于Anaconda的服务器在国外,默认源为Anaconda.org,国内下载相应资源缓慢,所以需要设置国内Anaconda镜像源。
网址:
https://mirror.tuna.tsinghua.edu.cn/help/anaconda/
#清华大学源
https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/
https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/pytorch/
网址:
https://mirrors.ustc.edu.cn/
#中国科学技术大学源
https://mirrors.ustc.edu.cn/anaconda/pkgs/main/
https://mirrors.ustc.edu.cn/anaconda/pkgs/free/
https://mirrors.ustc.edu.cn/anaconda/cloud/conda-forge/
#清华大学源
https://pypi.tuna.tsinghua.edu.cn/simple
#阿里巴巴源
https://pypi.doubanio.com/simple
#中国科学计数大学源
https://pypi.mirrors.ustc.edu.cn/simple/
#豆瓣源
https://pypi.doubanio.com/simple
pip install pandas -i https://pypi.tuna.tsinghua.edu.cn/simple#使用清华源下载pandas包
以配置清华大学源为例:
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
conda config --set show_channel_urls yes
conda config --set show_channel_urls yes
的作用是显示包的安装来源,如下:
linux下打开/home/xx/.condarc文件,添加下面内容保存即可:
channels:
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
- defaults
show_channel_urls: true
windows下默认无.condarc文件,需要 conda config --set show_channel_urls yes先生成,然后添加上面的内容。
conda config --show channels
conda config --get channels
默认源优先级已降至最低,新加入的清华源优先级最高。
直接删除.condarc文件。
conda config --remove channels channels_Name
conda config --remove channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
conda config --remove channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
conda config -h
conda info
conda config --show
将conda更新为最新版:
conda update -n base conda
conda update anaconda
更新python到当前系列的最新版,当前为python2,则只能更新到python2中最高版本,而不能更新到python3。
conda update python
conda list
输出四列,Name(包名称)、Version(包版本号)、Build(包创建者)、Channel(包下载来源)。
conda list PACKAGE_NAME
conda list -n ENV_NAME
conda list -n python2.7#查看环境python2.7下安装的所有包
conda search PACKAGE_NAME
conda search pandas#以pandas为例
conda search “PKGNAME [version=‘>=1.0.0,<1.1’]”
conda search "pandas [version='>=1.0.0,<1.1']"#搜索版本处于1.0.0及1.1之间的pandas
conda install PACKAGE_NAME默认安装在当前激活的环境,安装最新版。
conda install pandas#默认安装最新版本
conda install PACKAGE_NAME=VETSION_CODE
conda install pandas=1.1.1#安装1.1.1版的pandas
conda install “PACKAGE_NAME[version=‘1.0.4 |1.1.1’]”
conda install "pandas[version='1.0.4 |1.1.1']"#安装pandas 1.0.4版或者1.1.1版
conda install “PACKAGE_NAME>1.0.4,<1.1.1”
conda install "pandas>1.0.4,<1.1.1"#安装版本处于1.0.4到1.1.1之间的pandas
conda config --set always_yes yes
默认情况下为conda config --set always_yes false,也就是安装过程中会请求是否继续安装,设置为yes则不再弹出请求。
conda install -n ENV_NAME PACKAGE_NAME
可以这样做,但是完全没必要,建议先激活需要安装的环境,然后再安装。
conda install -n python2.7 pandas#将pandas安装在环境python2.7中
conda update PACKAGE_NAME
conda update pandas
conda update -n ENV_NAME PACKAGE_NAME
conda remove/uninstall PACKAGE_NAMEremove和uninstall都可以。
conda remove pandas
以下三种方法均可以:
conda info -e
conda info --envs
conda env list
conda create --name ENVNAME python=3.6
conda create -y -n python2.7 python=2.7.7
-y#-y, --yes Do not ask for confirmation.即安装过程无需输入y确认
-n python2.7#设置环境名称为python2.7
python=2.7.7#环境的版本为python=2.7.7,可通过conda search python检索可安装的版本号
conda info -e
查看当前所有的conda创建环境。
conda activate python2.7
conda 4.6后的版本,激活environment使用 conda activate
conda 4.6前的版本,激活environment使用:
Windows: activate
Linux and macOS: source activate
conda deactivate
将一个环境拷贝一份,二者配置一样,但是可以独立操作:
conda create --clone python2.7 --name new_python2.7#将环境python2.7克隆一个new_python2.7
conda remove --name new_python2.7 --all#将环境new_python2.7删除
https://github.com/conda/conda
https://conda.io/projects/conda/en/latest/index.html#