pip的安装和使用
1、 pip 是一个现代的,通用的 Python 包管理工具。提供了对 Python 包的查找、下载、安装、卸载的功能。便于我们对Python的资源包进行管理。
2、注:pip 已内置于 Python 3.4 和 2.7 及以上版本,其他版本需另行安装。
3、在安装Python时,会自动安装并下载pip
4、中Windows命令行里,输入 pip -v 可以查看pip的版本
1、 安装
测试pip是否安装
C:\Users\Administrator>python
Python 3.12.5 (tags/v3.12.5:ff3bc82, Aug 6 2024, 20:45:27) [MSC v.1940 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> ^Z
C:\Users\Administrator>pip -v
Usage:
pip <command> [options]
如果没有在安装中 勾选 Add Python 3.12.5 to PATH ,就手动配置pip的环境变量
我的电脑 – 右击 – 属性 – 高级系统设置 – 环境变量(N) – Path – 编辑 – 新建(N)
pip的路径
2、使用
2.1 路径
C:\Users\Administrator>e:
E:\>cd \soft\py\Scripts
E:\soft\py\Scripts>dir
驱动器 E 中的卷没有标签。
卷的序列号是 4074-73D8
E:\soft\py\Scripts 的目录
2024/09/23 周一 16:52 <DIR> .
2024/09/23 周一 16:52 <DIR> ..
2024/09/23 周一 16:52 108,380 pip.exe
2024/09/23 周一 16:52 108,380 pip3.12.exe
2024/09/23 周一 16:52 108,380 pip3.exe
3 个文件 325,140 字节
2 个目录 157,761,290,240 可用字节
2.2 管理python包
- pip install <包名> 安装指定的包
- pip uninstall <包名> 删除指定的包
- pip list 显示已经安装的包
- pip freeze 显示已经安装的 包,并且以指定的格式显示
E:\soft\py\Scripts>pip install ipython
...
E:\soft\py\Scripts>pip freeze
asttokens==2.4.1
colorama==0.4.6
decorator==5.1.1
executing==2.1.0
ipython==8.27.0
jedi==0.19.1
matplotlib-inline==0.1.7
parso==0.8.4
prompt_toolkit==3.0.47
pure_eval==0.2.3
Pygments==2.18.0
six==1.16.0
stack-data==0.6.3
traitlets==5.14.3
wcwidth==0.2.13
E:\soft\py\Scripts>pip list
Package Version
----------------- -------
asttokens 2.4.1
colorama 0.4.6
decorator 5.1.1
executing 2.1.0
ipython 8.27.0
jedi 0.19.1
matplotlib-inline 0.1.7
parso 0.8.4
pip 24.2
prompt_toolkit 3.0.47
pure_eval 0.2.3
Pygments 2.18.0
six 1.16.0
stack-data 0.6.3
traitlets 5.14.3
wcwidth 0.2.13
2.3 pip下载第三方包和其依赖包
2.3.1 下载requests包,且保存在demo文件夹里面
E:\soft\py\Scripts>pip download -d demo requests
Collecting requests
Downloading requests-2.32.3-py3-none-any.whl.metadata (4.6 kB)
Collecting charset-normalizer<4,>=2 (from requests)
Downloading charset_normalizer-3.3.2-cp312-cp312-win_amd64.whl.metadata (34 kB)
Collecting idna<4,>=2.5 (from requests)
Downloading idna-3.10-py3-none-any.whl.metadata (10 kB)
Collecting urllib3<3,>=1.21.1 (from requests)
Downloading urllib3-2.2.3-py3-none-any.whl.metadata (6.5 kB)
Collecting certifi>=2017.4.17 (from requests)
Downloading certifi-2024.8.30-py3-none-any.whl.metadata (2.2 kB)
Downloading requests-2.32.3-py3-none-any.whl (64 kB)
Downloading certifi-2024.8.30-py3-none-any.whl (167 kB)
Downloading charset_normalizer-3.3.2-cp312-cp312-win_amd64.whl (100 kB)
Downloading idna-3.10-py3-none-any.whl (70 kB)
Downloading urllib3-2.2.3-py3-none-any.whl (126 kB)
Saved e:\soft\py\scripts\demo\requests-2.32.3-py3-none-any.whl
Saved e:\soft\py\scripts\demo\certifi-2024.8.30-py3-none-any.whl
Saved e:\soft\py\scripts\demo\charset_normalizer-3.3.2-cp312-cp312-win_amd64.whl
Saved e:\soft\py\scripts\demo\idna-3.10-py3-none-any.whl
Saved e:\soft\py\scripts\demo\urllib3-2.2.3-py3-none-any.whl
Successfully downloaded requests certifi charset-normalizer idna urllib3
下载后
2.3.2 创建一个requirements.txt
2.3.3 requirement.txt内容
urllib3-2.2.3-py3-none-any.whl
idna-3.10-py3-none-any.whl
charset_normalizer-3.3.2-cp312-cp312-win_amd64.whl
certifi-2024.8.30-py3-none-any.whl
requests-2.32.3-py3-none-any.whl
# 进入demo文件夹目录路径
E:\soft\py\Scripts>cd demo
# 离线安装刚才下载的安装包
E:\soft\py\Scripts\demo>pip install -r requirements.txt
Processing e:\soft\py\scripts\demo\urllib3-2.2.3-py3-none-any.whl (from -r requirements.txt (line 1))
Processing e:\soft\py\scripts\demo\idna-3.10-py3-none-any.whl (from -r requirements.txt (line 2))
Processing e:\soft\py\scripts\demo\charset_normalizer-3.3.2-cp312-cp312-win_amd64.whl (from -r requirements.txt (line 3))
Processing e:\soft\py\scripts\demo\certifi-2024.8.30-py3-none-any.whl (from -r requirements.txt (line 4))
Processing e:\soft\py\scripts\demo\requests-2.32.3-py3-none-any.whl (from -r requirements.txt (line 5))
Installing collected packages: urllib3, idna, charset-normalizer, certifi, requests
Successfully installed certifi-2024.8.30 charset-normalizer-3.3.2 idna-3.10 requests-2.32.3 urllib3-2.2.3
E:\soft\py\Scripts\demo>pip list
Package Version
------------------ ---------
asttokens 2.4.1
certifi 2024.8.30
charset-normalizer 3.3.2
colorama 0.4.6
decorator 5.1.1
executing 2.1.0
idna 3.10
ipython 8.27.0
jedi 0.19.1
matplotlib-inline 0.1.7
parso 0.8.4
pip 24.2
prompt_toolkit 3.0.47
pure_eval 0.2.3
Pygments 2.18.0
requests 2.32.3
six 1.16.0
stack-data 0.6.3
traitlets 5.14.3
urllib3 2.2.3
wcwidth 0.2.13
2.4 修改pip下载源
E:\soft\py\Scripts>pip install ipython -i https://pypi.douban.com/simple