1、先确认是否安装了
通过pip安装
pip是python的一个模块,在使用之前,要确认一下该模块是否存在。可以在命令行(win+R显示对话框,输入CMD)中输入pip list,如果正常显示已安装的包则说明pip可用。
C:\Users>pip list
Package Version
--------------- ----------
beautifulsoup4 4.9.1
Django 3.0.5
lxml 4.5.0
matplotlib 3.1.2
numpy 1.18.1
如果显示找不到pip命令,则要手动安装一下,即在命令行输入以下内容:
C:\Users>python -m ensurepip
为了保证后面模块的正常安装,如果不是最新的版本可以使用如下命令把pip升级一下:
C:\Users>pip install --upgrade pip
出现这个错误,估计我只能手动安装了。
至此,我们的pip命令就可以正常使用了,pip作为模块安装命令,使用还是比较多的,并且其参数也较多,配置很灵活,常使用如下方法安装模块:
1)正常在线安装
2)离线包安装
正常在线安装
正常在线安装是比较常用的安装方法,我们需要什么模块,直接pip install 模块名即可,想卸载已安装模块,直接pip uninstall 模块,下面以numpy模块为例进行演示(先安装,再卸载):
pip install numpy
pip uninstall numpy
C:\Users\lenovo>pip install numpy
Collecting numpy
Downloading numpy-1.26.4-cp310-cp310-win_amd64.whl.metadata (61 kB)
---------------------------------------- 61.0/61.0 kB 651.3 kB/s eta 0:00:00
Downloading numpy-1.26.4-cp310-cp310-win_amd64.whl (15.8 MB)
---------------------------------------- 15.8/15.8 MB 18.2 MB/s eta 0:00:00
Installing collected packages: numpy
Successfully installed numpy-1.26.4
C:\Users\lenovo>
C:\Users\lenovo>pip install xlrd
Collecting xlrd
Downloading xlrd-2.0.1-py2.py3-none-any.whl.metadata (3.4 kB)
Downloading xlrd-2.0.1-py2.py3-none-any.whl (96 kB)
---------------------------------------- 96.5/96.5 kB 918.0 kB/s eta 0:00:00
Installing collected packages: xlrd
Successfully installed xlrd-2.0.1
C:\Users\lenovo>pip install pandas
Collecting pandas
Downloading pandas-2.2.2-cp310-cp310-win_amd64.whl.metadata (19 kB)
Requirement already satisfied: numpy>=1.22.4 in d:\python\lib\site-packages (from pandas) (1.26.4)
Collecting python-dateutil>=2.8.2 (from pandas)
Downloading python_dateutil-2.9.0.post0-py2.py3-none-any.whl.metadata (8.4 kB)
Collecting pytz>=2020.1 (from pandas)
Downloading pytz-2024.1-py2.py3-none-any.whl.metadata (22 kB)
Collecting tzdata>=2022.7 (from pandas)
Downloading tzdata-2024.1-py2.py3-none-any.whl.metadata (1.4 kB)
Collecting six>=1.5 (from python-dateutil>=2.8.2->pandas)
Downloading six-1.16.0-py2.py3-none-any.whl.metadata (1.8 kB)
Downloading pandas-2.2.2-cp310-cp310-win_amd64.whl (11.6 MB)
---------------------------------------- 11.6/11.6 MB 16.4 MB/s eta 0:00:00
Downloading python_dateutil-2.9.0.post0-py2.py3-none-any.whl (229 kB)
---------------------------------------- 229.9/229.9 kB 14.6 MB/s eta 0:00:00
Downloading pytz-2024.1-py2.py3-none-any.whl (505 kB)
---------------------------------------- 505.5/505.5 kB 16.0 MB/s eta 0:00:00
Downloading tzdata-2024.1-py2.py3-none-any.whl (345 kB)
---------------------------------------- 345.4/345.4 kB 20.9 MB/s eta 0:00:00
Downloading six-1.16.0-py2.py3-none-any.whl (11 kB)
Installing collected packages: pytz, tzdata, six, python-dateutil, pandas
安装后出现了pip list :
输完一次后需要等一下CMD运行
pip命令补全
有时如果没有把pip路径添加到环境变量里,直接使用pip install xxx会出错,这时,我们可以把命令补全,即:
python -m pip install 模块名
python -m pip uninstall 模块名