文章目录
1. pip简介
一种用于管理Python包的工具。
pip文档:User Guide - pip documentation v22.1.1
在virtualenv或conda create的虚拟环境下直接使用pip,就可以将包安装在指定环境下。如果不在Python环境下,需要在命令行前面加上 python -m(如果Python解释器不在默认路径,就用完整的解释器路径,举例:/home/myusername/anaconda3/envs/envname/bin/python -m)
2. pip install
- 直接安装包,示例代码:
pip install numpy(一般建议参考包官网及pypi官网,了解具体的包安装方式) - 可以直接通过这一方式安装最新版本的常用包(不便直接使用pip install的包一般都在其官网有写具体的安装方式,部分包我撰写过相应的安装指南)
- numpy:
pip install numpy - scipy:
pip install scipy官网安装教程:SciPy - Installation - scikit-learn (sklearn):
pip install scikit-learn官网下载教程:Installing scikit-learn — scikit-learn 1.1.1 documentation - matplotlib:
pip install matplotlib官网入门教程:Getting started — Matplotlib 3.5.3 documentation - wordcloud:
pip install wordcloudpypi网站:wordcloud · PyPI(虽然网页上只说测试到了Python3.7,其实Python3.8也能用),文档:WordCloud for Python documentation — wordcloud 1.8.1 documentation,官方博文:A Wordcloud in Python,GitHub项目:amueller/word_cloud: A little word cloud generator in Python - gensim:
pip install gensim官网:Gensim: Topic modelling for humans - pyLDAvis:
pip install pyldavis官方文档:pyLDAvis — pyLDAvis 2.1.2 documentation - tqdm:
pip install tqdm官方GitHub项目:tqdm/tqdm: A Fast, Extensible Progress Bar for Python and CLI - seaborn:
pip install seaborn官网下载教程:Installing and getting started — seaborn 0.11.2 documentation - sentence_transformers:
pip install sentence_transformers官网:sentence-transformers (Sentence Transformers) - pybind11:
pip install pybind11官网下载教程:Installing the library — pybind11 documentation
- numpy:
- 指定安装某一版本的包,示例代码:
pip install numpy==1.14 - 通过源码安装包(以textgen包1为例):
git clone https://github.com/shibing624/textgen.git
cd textgen
python setup.py install
通过源码安装包,在源码更新时同时更新包的方法:
(待补)
- 如果出现网络问题等,无法直接通过包名下载,也可以将指定包的whl文件下载后,通过pip直接安装whl文件,示例代码:
pip install torch_scatter-2.0.9-cp39-cp39-linux_x86_64.whl
另一种方式是,如果能确定操作系统和版本是一致的,也可以在另一个(能安装这个包的)环境里安装好,然后把虚拟环境中的整个包挪过来。反正有时是能用的。 - 参数
--upgrade或-U更新包到最新版本,示例代码:pip install --upgrade numpy(文档地址:https://pip.pypa.io/en/stable/cli/pip_install/#cmdoption-U)- 参数
--pre下载pre-release或development版本。(默认情况下pip会搜索stable版本的包) 示例代码:pip install --pre dgl-cu102 -f https://data.dgl.ai/wheels-test/repo.html - 参数
-i指定源,示例代码:pip install -i https://mirrors.tuna.tsinghua.edu.cn/pypi/web/simple cogdl
大陆常用镜像:https://pypi.tuna.tsinghua.edu.cn/simplepypi | 镜像站使用帮助 | 清华大学开源软件镜像站 | Tsinghua Open Source Mirror - 参数
-r指定requirements.txt,示例代码:pip install -r requirements.txt -i https://pypi.tuna.tsinghua.edu.cn/simple(使用了清华源) - 参数
-e, --editable <path/url>以editable mode,从本地项目路径或一个VCS URL安装包。示例代码:pip install -e . - 参数
--user安装到Python用户的install directory里,一般来说是~/.local/,Windows平台一般是%APPDATA%Python,可以查看Python文档里的site.USER_BASE(对应网址:site — Site-specific configuration hook — Python 3.10.4 documentation)来了解详情。(文档地址:https://pip.pypa.io/en/stable/cli/pip_install/#install-user) - 参数
-i, --index-url <url>Python包搜索位置。默认值:https://pypi.org/simple - 参数
--extra-index-url <url>--index-url的补充超链接 - 参数
-f-可以指定在某网页中搜索下载指定版本的包(一般都是包官方给出的安装方式),示例代码:pip install torch-scatter -f https://data.pyg.org/whl/torch-1.10.0+cu102.html
3. pip show 展示包的信息
示例代码:pip show numpy
打印信息:
Name: numpy
Version: 1.21.2
Summary: NumPy is the fundamental package for array computing with Python.
Home-page: https://www.numpy.org
Author: Travis E. Oliphant et al.
Author-email:
License: BSD
Location: /home/myusername/anaconda3/envs/envname/lib/python3.8/site-packages
Requires:
Required-by: transformers, torchvision, torch, scipy, pandas, mkl-random, mkl-fft, matplotlib
4. pip list 打印已安装的包的简略信息
打印的示例(部分):
Package Version
----------------------------- ----------
anyio 3.4.0
argon2-cffi 20.1.0
async-generator 1.10
attrs 21.2.0
Babel 2.9.1
backcall 0.2.0
backports.functools-lru-cache 1.6.4
5. pip cache dir 打印pip的cache文件夹
6. pip cache purge 手动清除缓存2
7. which pip 打印pip的路径
8. 永久修改pip下载源(本文以清华源为例,其他源改成对应链接即可)
- 方法一:在
.pip/pip.conf(Linux/Unix)或%HOME%\pip\pip.ini(Windows)文件中添加以下内容:
[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple
- 方法二:在
.condarc中增加如下内容3:
show_channel_urls: true
default_channels:
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/r
- https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/msys2
custom_channels:
conda-forge: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
msys2: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
bioconda: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
menpo: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
pytorch: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
pytorch-lts: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
simpleitk: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud
deepmodeling: https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/
9. 镜像
清华镜像:https://mirrors.tuna.tsinghua.edu.cn/help/pypi/
本文详细介绍了如何在Python环境中使用pip进行包的安装、升级、指定版本和源码安装,包括virtualenv、conda环境下的操作,以及pip配置、缓存管理和常用库如numpy、scipy、matplotlib等的安装教程。
30万+





