日记首页
|
列表
|
添加日记
|
管理登陆
标题:轻松入门:Python中的pip命令安装与使用
<div class="dpu8C _2kCxD " style="max-width: 100%; overflow-x: visible; font-size: 18px; line-height: 30px; color: rgb(34, 34, 34); margin-top: 28px; font-family: arial;"><span class="bjh-h3" style="overflow-y: auto; max-width: 100%; display: inline-block; position: relative; padding-left: 9px; color: rgb(0, 0, 0);"><span class="dqa-highlight-violet" style="font-size: 10.5pt; background-image: initial; background-position: 0px 0px; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; color: rgb(51, 51, 51); font-family: -apple-system, Arial, Helvetica, sans-serif;">使用`pip`命令查看已安装的库,可以使用`pip list`命令。</span><span style="font-size: 10.5pt; color: rgb(51, 51, 51); font-family: -apple-system, Arial, Helvetica, sans-serif;">这个命令会列出所有已安装的库及其版本号。</span></span></div><div class="dpu8C _2kCxD " style="max-width: 100%; overflow-x: visible; font-size: 18px; line-height: 30px; color: rgb(34, 34, 34); margin-top: 28px; font-family: arial;"><span class="bjh-h3" style="overflow-y: auto; max-width: 100%; display: inline-block; position: relative; padding-left: 9px; font-weight: 700; color: rgb(0, 0, 0);">什么是pip</span></div><div class="dpu8C _2kCxD " style="max-width: 100%; overflow-x: visible; font-size: 18px; line-height: 30px; color: rgb(34, 34, 34); margin-top: 28px; font-family: arial;"><span class="bjh-p" style="overflow-y: auto; max-width: 100%;">pip是Python的包管理工具,它的全称是"pip installs packages",意为"安装包"。</span></div><div class="dpu8C _2kCxD " style="max-width: 100%; overflow-x: visible; font-size: 18px; line-height: 30px; color: rgb(34, 34, 34); margin-top: 28px; font-family: arial;"><span class="bjh-p" style="overflow-y: auto; max-width: 100%;">通过pip可以方便地安装、卸载和管理Python的第三方库。在Python的生态系统中,有大量的第三方库提供了各种功能和工具,通过pip可以方便地获取这些库并在自己的项目中使用。</span></div><div class="dpu8C _2kCxD " style="max-width: 100%; overflow-x: visible; font-size: 18px; line-height: 30px; color: rgb(34, 34, 34); margin-top: 24px; font-family: arial;"><span class="bjh-h3" style="overflow-y: auto; max-width: 100%; display: inline-block; position: relative; padding-left: 9px; font-weight: 700; color: rgb(0, 0, 0);">pip的安装</span></div><div class="dpu8C _2kCxD " style="max-width: 100%; overflow-x: visible; font-size: 18px; line-height: 30px; color: rgb(34, 34, 34); margin-top: 24px; font-family: arial;"><span class="bjh-p" style="overflow-y: auto; max-width: 100%;">pip在Python 3.4版本之后已经成为Python的标准模块,所以在较新版本的Python中一般已经自带了pip。</span></div><div class="dpu8C _2kCxD " style="max-width: 100%; overflow-x: visible; font-size: 18px; line-height: 30px; color: rgb(34, 34, 34); margin-top: 24px; font-family: arial;"><span class="bjh-p" style="overflow-y: auto; max-width: 100%;">你可以通过以下命令来检查是否已经安装了pip:</span></div><div class="dpu8C _2kCxD " style="max-width: 100%; overflow-x: visible; font-size: 18px; line-height: 30px; color: rgb(34, 34, 34); margin-top: 24px; font-family: arial;"><span class="bjh-blockquote" style="overflow-y: auto; max-width: 100%; display: block; position: relative; padding-left: 30px; min-height: 24px; color: rgb(153, 153, 153);"><span class="bjh-p" style="max-width: 100%;"><span class="bjh-strong" style="max-width: 100%; font-weight: 700;">pip lit</span></span></span><span class="bjh-blockquote" style="overflow-y: auto; max-width: 100%; display: block; position: relative; padding-left: 30px; min-height: 24px; color: rgb(153, 153, 153);"><span class="bjh-p" style="max-width: 100%;"><span class="bjh-strong" style="max-width: 100%; font-weight: 700;"><span style="color: rgb(51, 51, 51); font-family: -apple-system, Arial, Helvetica, sans-serif; font-size: 14px; font-weight: 400;">3.8.10版本这个命令会列出所有已安装的库及其版本号。</span></span></span></span><span class="bjh-blockquote" style="overflow-y: auto; max-width: 100%; display: block; position: relative; padding-left: 30px; min-height: 24px; color: rgb(153, 153, 153);"><span class="bjh-p" style="max-width: 100%;"><span class="bjh-strong" style="max-width: 100%; font-weight: 700;">pip --version</span></span></span></div><div class="dpu8C _2kCxD " style="max-width: 100%; overflow-x: visible; font-size: 18px; line-height: 30px; color: rgb(34, 34, 34); margin-top: 24px; font-family: arial;"><span class="bjh-p" style="overflow-y: auto; max-width: 100%;">如果已经安装了pip,会显示pip的版本信息。如果没有安装,可以通过以下方法安装pip:</span></div><div class="dpu8C _2kCxD " style="max-width: 100%; overflow-x: visible; font-size: 18px; line-height: 30px; color: rgb(34, 34, 34); margin-top: 24px; font-family: arial;"><span class="bjh-ul" style="overflow-y: auto; max-width: 100%; color: rgb(51, 51, 51); list-style-type: disc; display: block; line-height: 27px; padding-left: 26px;"><span class="bjh-li" style="max-width: 100%; display: list-item; margin-bottom: 0px;"><span class="bjh-p" style="max-width: 100%;"><span class="bjh-strong" style="max-width: 100%; font-weight: 700;">Windows系统</span></span></span></span><span class="bjh-p" style="overflow-y: auto; max-width: 100%;">- 打开命令行窗口(按下Win + R,然后输入cmd并按Enter键)。</span></div><div class="dpu8C _2kCxD " style="max-width: 100%; overflow-x: visible; font-size: 18px; line-height: 30px; color: rgb(34, 34, 34); margin-top: 24px; font-family: arial;"><span class="bjh-p" style="overflow-y: auto; max-width: 100%;">- 在命令行中输入以下命令并按Enter键:</span></div><div class="dpu8C _2kCxD " style="max-width: 100%; overflow-x: visible; font-size: 18px; line-height: 30px; color: rgb(34, 34, 34); margin-top: 24px; font-family: arial;"><span class="bjh-blockquote" style="overflow-y: auto; max-width: 100%; display: block; position: relative; padding-left: 30px; min-height: 24px; color: rgb(153, 153, 153);"><span class="bjh-p" style="max-width: 100%;">python get-pip.py</span></span></div><div class="dpu8C _2kCxD " style="max-width: 100%; overflow-x: visible; font-size: 18px; line-height: 30px; color: rgb(34, 34, 34); margin-top: 24px; font-family: arial;"><span class="bjh-p" style="overflow-y: auto; max-width: 100%;">- 等待安装完成。</span></div><div class="dpu8C _2kCxD " style="max-width: 100%; overflow-x: visible; font-size: 18px; line-height: 30px; color: rgb(34, 34, 34); margin-top: 24px; font-family: arial;"><span class="bjh-p" style="overflow-y: auto; max-width: 100%;"><span class="bjh-strong" style="max-width: 100%; color: rgb(51, 51, 51); font-weight: 700;">以上是Python2.x安装pip方法,Python3.X默认已安装pip。</span></span></div><div class="dpu8C _2kCxD " style="max-width: 100%; overflow-x: visible; font-size: 18px; line-height: 30px; color: rgb(34, 34, 34); margin-top: 24px; font-family: arial;"><span class="bjh-ul" style="overflow-y: auto; max-width: 100%; color: rgb(51, 51, 51); list-style-type: disc; display: block; line-height: 27px; padding-left: 26px;"><span class="bjh-li" style="max-width: 100%; display: list-item; margin-bottom: 0px;"><span class="bjh-p" style="max-width: 100%;"><span class="bjh-strong" style="max-width: 100%; font-weight: 700;">macOS系统和Linux系统</span></span></span></span><span class="bjh-p" style="overflow-y: auto; max-width: 100%;">- 打开终端。</span></div><div class="dpu8C _2kCxD " style="max-width: 100%; overflow-x: visible; font-size: 18px; line-height: 30px; color: rgb(34, 34, 34); margin-top: 24px; font-family: arial;"><span class="bjh-p" style="overflow-y: auto; max-width: 100%;">- 在终端中输入以下命令并按Enter键:</span></div><div class="dpu8C _2kCxD " style="max-width: 100%; overflow-x: visible; font-size: 18px; line-height: 30px; color: rgb(34, 34, 34); margin-top: 24px; font-family: arial;"><span class="bjh-blockquote" style="overflow-y: auto; max-width: 100%; display: block; position: relative; padding-left: 30px; min-height: 24px; color: rgb(153, 153, 153);"><span class="bjh-p" style="max-width: 100%;">sudo easy_install pip</span></span></div><div class="dpu8C _2kCxD " style="max-width: 100%; overflow-x: visible; font-size: 18px; line-height: 30px; color: rgb(34, 34, 34); margin-top: 24px; font-family: arial;"><span class="bjh-p" style="overflow-y: auto; max-width: 100%;">- 输入管理员密码(根据提示)。</span></div><div class="dpu8C _2kCxD " style="max-width: 100%; overflow-x: visible; font-size: 18px; line-height: 30px; color: rgb(34, 34, 34); margin-top: 24px; font-family: arial;"><span class="bjh-p" style="overflow-y: auto; max-width: 100%;">- 等待安装完成。</span></div><div class="dpu8C _2kCxD " style="max-width: 100%; overflow-x: visible; font-size: 18px; line-height: 30px; color: rgb(34, 34, 34); margin-top: 24px; font-family: arial;"><span class="bjh-h3" style="overflow-y: auto; max-width: 100%; display: inline-block; position: relative; padding-left: 9px; font-weight: 700; color: rgb(0, 0, 0);">pip的使用</span></div><div class="dpu8C _2kCxD " style="max-width: 100%; overflow-x: visible; font-size: 18px; line-height: 30px; color: rgb(34, 34, 34); margin-top: 24px; font-family: arial;"><span class="bjh-p" style="overflow-y: auto; max-width: 100%;">安装完pip之后,就可以通过pip命令来管理和安装Python的第三方库了。</span></div><div class="dpu8C _2kCxD " style="max-width: 100%; overflow-x: visible; font-size: 18px; line-height: 30px; color: rgb(34, 34, 34); margin-top: 24px; font-family: arial;"><span class="bjh-p" style="overflow-y: auto; max-width: 100%;">以下是一些常见的pip命令及其用法:</span></div><div class="dpu8C _2kCxD " style="max-width: 100%; overflow-x: visible; font-size: 18px; line-height: 30px; color: rgb(34, 34, 34); margin-top: 24px; font-family: arial;"><span class="bjh-p" style="overflow-y: auto; max-width: 100%;"><span class="bjh-strong" style="max-width: 100%; color: rgb(51, 51, 51); font-weight: 700;">安装库</span></span></div><div class="dpu8C _2kCxD " style="max-width: 100%; overflow-x: visible; font-size: 18px; line-height: 30px; color: rgb(34, 34, 34); margin-top: 24px; font-family: arial;"><span class="bjh-p" style="overflow-y: auto; max-width: 100%;">- 安装指定库的最新版本:</span></div><div class="dpu8C _2kCxD " style="max-width: 100%; overflow-x: visible; font-size: 18px; line-height: 30px; color: rgb(34, 34, 34); margin-top: 24px; font-family: arial;"><span class="bjh-blockquote" style="overflow-y: auto; max-width: 100%; display: block; position: relative; padding-left: 30px; min-height: 24px; color: rgb(153, 153, 153);"><span class="bjh-p" style="max-width: 100%;">pip install <package_name></span></span></div><div class="dpu8C _2kCxD " style="max-width: 100%; overflow-x: visible; font-size: 18px; line-height: 30px; color: rgb(34, 34, 34); margin-top: 24px; font-family: arial;"><span class="bjh-p" style="overflow-y: auto; max-width: 100%;">- 安装指定库的指定版本:</span></div><div class="dpu8C _2kCxD " style="max-width: 100%; overflow-x: visible; font-size: 18px; line-height: 30px; color: rgb(34, 34, 34); margin-top: 24px; font-family: arial;"><span class="bjh-blockquote" style="overflow-y: auto; max-width: 100%; display: block; position: relative; padding-left: 30px; min-height: 24px; color: rgb(153, 153, 153);"><span class="bjh-p" style="max-width: 100%;">pip install <package_name>==<version></span></span></div><div class="dpu8C _2kCxD " style="max-width: 100%; overflow-x: visible; font-size: 18px; line-height: 30px; color: rgb(34, 34, 34); margin-top: 24px; font-family: arial;"><span class="bjh-p" style="overflow-y: auto; max-width: 100%;">- 安装多个库(一次安装多个库,用空格分隔):</span></div><div class="dpu8C _2kCxD " style="max-width: 100%; overflow-x: visible; font-size: 18px; line-height: 30px; color: rgb(34, 34, 34); margin-top: 24px; font-family: arial;"><span class="bjh-blockquote" style="overflow-y: auto; max-width: 100%; display: block; position: relative; padding-left: 30px; min-height: 24px; color: rgb(153, 153, 153);"><span class="bjh-p" style="max-width: 100%;">pip install <package1_name> <package2_name></span></span></div><div class="dpu8C _2kCxD " style="max-width: 100%; overflow-x: visible; font-size: 18px; line-height: 30px; color: rgb(34, 34, 34); margin-top: 24px; font-family: arial;"><span class="bjh-p" style="overflow-y: auto; max-width: 100%;"><span class="bjh-strong" style="max-width: 100%; color: rgb(51, 51, 51); font-weight: 700;">升级库</span></span></div><div class="dpu8C _2kCxD " style="max-width: 100%; overflow-x: visible; font-size: 18px; line-height: 30px; color: rgb(34, 34, 34); margin-top: 24px; font-family: arial;"><span class="bjh-p" style="overflow-y: auto; max-width: 100%;">- 升级指定库的最新版本:</span></div><div class="dpu8C _2kCxD " style="max-width: 100%; overflow-x: visible; font-size: 18px; line-height: 30px; color: rgb(34, 34, 34); margin-top: 24px; font-family: arial;"><span class="bjh-blockquote" style="overflow-y: auto; max-width: 100%; display: block; position: relative; padding-left: 30px; min-height: 24px; color: rgb(153, 153, 153);"><span class="bjh-p" style="max-width: 100%;">pip install --upgrade <package_name></span></span></div><div class="dpu8C _2kCxD " style="max-width: 100%; overflow-x: visible; font-size: 18px; line-height: 30px; color: rgb(34, 34, 34); margin-top: 24px; font-family: arial;"><span class="bjh-p" style="overflow-y: auto; max-width: 100%;"><span class="bjh-strong" style="max-width: 100%; color: rgb(51, 51, 51); font-weight: 700;">卸载库</span></span></div><div class="dpu8C _2kCxD " style="max-width: 100%; overflow-x: visible; font-size: 18px; line-height: 30px; color: rgb(34, 34, 34); margin-top: 24px; font-family: arial;"><span class="bjh-p" style="overflow-y: auto; max-width: 100%;">- 卸载指定库:</span></div><div class="dpu8C _2kCxD " style="max-width: 100%; overflow-x: visible; font-size: 18px; line-height: 30px; color: rgb(34, 34, 34); margin-top: 24px; font-family: arial;"><span class="bjh-blockquote" style="overflow-y: auto; max-width: 100%; display: block; position: relative; padding-left: 30px; min-height: 24px; color: rgb(153, 153, 153);"><span class="bjh-p" style="max-width: 100%;">pip uninstall <package_name></span></span></div><div class="dpu8C _2kCxD " style="max-width: 100%; overflow-x: visible; font-size: 18px; line-height: 30px; color: rgb(34, 34, 34); margin-top: 24px; font-family: arial;"><span class="bjh-p" style="overflow-y: auto; max-width: 100%;"><span class="bjh-strong" style="max-width: 100%; color: rgb(51, 51, 51); font-weight: 700;">查看已安装的库</span></span></div><div class="dpu8C _2kCxD " style="max-width: 100%; overflow-x: visible; font-size: 18px; line-height: 30px; color: rgb(34, 34, 34); margin-top: 24px; font-family: arial;"><span class="bjh-p" style="overflow-y: auto; max-width: 100%;">- 查看已安装的所有库:</span></div><div class="dpu8C _2kCxD " style="max-width: 100%; overflow-x: visible; font-size: 18px; line-height: 30px; color: rgb(34, 34, 34); margin-top: 24px; font-family: arial;"><span class="bjh-blockquote" style="overflow-y: auto; max-width: 100%; display: block; position: relative; padding-left: 30px; min-height: 24px; color: rgb(153, 153, 153);"><span class="bjh-p" style="max-width: 100%;">pip list</span></span></div><div class="dpu8C _2kCxD " style="max-width: 100%; overflow-x: visible; font-size: 18px; line-height: 30px; color: rgb(34, 34, 34); margin-top: 24px; font-family: arial;"><span class="bjh-p" style="overflow-y: auto; max-width: 100%;">- 查看指定库的信息:</span></div><div class="dpu8C _2kCxD " style="max-width: 100%; overflow-x: visible; font-size: 18px; line-height: 30px; color: rgb(34, 34, 34); margin-top: 24px; font-family: arial;"><span class="bjh-blockquote" style="overflow-y: auto; max-width: 100%; display: block; position: relative; padding-left: 30px; min-height: 24px; color: rgb(153, 153, 153);"><span class="bjh-p" style="max-width: 100%;">pip show <package_name></span></span><div class="dpu8C _2kCxD " style="max-width: 100%; overflow-x: visible; line-height: 30px; margin-top: 24px;"><span class="bjh-p" style="overflow-y: auto; max-width: 100%;"><span style="color: rgb(0, 0, 0); font-weight: 700;">pip的高级用法</span></span></div><div class="dpu8C _2kCxD " style="max-width: 100%; overflow-x: visible; line-height: 30px; margin-top: 24px;"><span class="bjh-p" style="overflow-y: auto; max-width: 100%;">pip还有一些高级的用法和参数,可以帮助我们更灵活地管理和配置Python的第三方库。以下是一些常见的高级用法:</span></div><div class="dpu8C _2kCxD " style="max-width: 100%; overflow-x: visible; line-height: 30px; margin-top: 24px;"><span class="bjh-p" style="overflow-y: auto; max-width: 100%;"><span class="bjh-strong" style="max-width: 100%; color: rgb(51, 51, 51); font-weight: 700;">导出已安装库的列表</span></span></div><div class="dpu8C _2kCxD " style="max-width: 100%; overflow-x: visible; line-height: 30px; margin-top: 24px;"><span class="bjh-blockquote" style="overflow-y: auto; max-width: 100%; display: block; position: relative; padding-left: 30px; min-height: 24px; color: rgb(153, 153, 153);"><span class="bjh-p" style="max-width: 100%;">pip freeze > requirements.txt</span></span></div><div class="dpu8C _2kCxD " style="max-width: 100%; overflow-x: visible; line-height: 30px; margin-top: 24px;"><span class="bjh-p" style="overflow-y: auto; max-width: 100%;">这个命令会将已安装的库及其版本号导出到一个名为requirements.txt的文件中,方便其他人或其他项目使用。</span></div><div class="dpu8C _2kCxD " style="max-width: 100%; overflow-x: visible; line-height: 30px; margin-top: 24px;"><span class="bjh-p" style="overflow-y: auto; max-width: 100%;"><span class="bjh-strong" style="max-width: 100%; color: rgb(51, 51, 51); font-weight: 700;">从requirements.txt文件安装库</span></span></div><div class="dpu8C _2kCxD " style="max-width: 100%; overflow-x: visible; line-height: 30px; margin-top: 24px;"><span class="bjh-blockquote" style="overflow-y: auto; max-width: 100%; display: block; position: relative; padding-left: 30px; min-height: 24px; color: rgb(153, 153, 153);"><span class="bjh-p" style="max-width: 100%;">pip install -r requirements.txt</span></span></div><div class="dpu8C _2kCxD " style="max-width: 100%; overflow-x: visible; line-height: 30px; margin-top: 24px;"><span class="bjh-p" style="overflow-y: auto; max-width: 100%;">这个命令会根据requirements.txt文件的内容,安装所需的库和对应的版本。</span></div><div class="dpu8C _2kCxD " style="max-width: 100%; overflow-x: visible; line-height: 30px; margin-top: 24px;"><span class="bjh-p" style="overflow-y: auto; max-width: 100%;"><span class="bjh-strong" style="max-width: 100%; color: rgb(51, 51, 51); font-weight: 700;">设置pip的下载源</span></span></div><div class="dpu8C _2kCxD " style="max-width: 100%; overflow-x: visible; line-height: 30px; margin-top: 24px;"><span class="bjh-blockquote" style="overflow-y: auto; max-width: 100%; display: block; position: relative; padding-left: 30px; min-height: 24px; color: rgb(153, 153, 153);"><span class="bjh-p" style="max-width: 100%;">pip install <package_name> -i <source_url></span></span></div><div class="dpu8C _2kCxD " style="max-width: 100%; overflow-x: visible; line-height: 30px; margin-top: 24px;"><span class="bjh-p" style="overflow-y: auto; max-width: 100%;">这个命令会从指定的源地址下载安装库。</span></div><div class="dpu8C _2kCxD " style="max-width: 100%; overflow-x: visible; line-height: 30px; margin-top: 24px;"><span class="bjh-p" style="overflow-y: auto; max-width: 100%;"><span class="bjh-strong" style="max-width: 100%; color: rgb(51, 51, 51); font-weight: 700;">配置pip源的默认地址</span></span></div><div class="dpu8C _2kCxD " style="max-width: 100%; overflow-x: visible; line-height: 30px; margin-top: 24px;"><span class="bjh-p" style="overflow-y: auto; max-width: 100%;">pip config set global.index-url <source_url></span></div><div class="dpu8C _2kCxD " style="max-width: 100%; overflow-x: visible; line-height: 30px; margin-top: 24px;"><span class="bjh-p" style="overflow-y: auto; max-width: 100%;">这个命令会将指定的源地址配置为pip的默认源,以后使用pip安装时会自动从这个源地址下载库。</span></div><div class="dpu8C _2kCxD " style="max-width: 100%; overflow-x: visible; line-height: 30px; margin-top: 24px;"><span class="bjh-h3" style="overflow-y: auto; max-width: 100%; display: inline-block; position: relative; padding-left: 9px; font-weight: 700; color: rgb(0, 0, 0);">常见问题及解决方法</span></div><div class="dpu8C _2kCxD " style="max-width: 100%; overflow-x: visible; line-height: 30px; margin-top: 24px;"><span class="bjh-ul" style="overflow-y: auto; max-width: 100%; color: rgb(51, 51, 51); list-style-type: disc; display: block; line-height: 27px; padding-left: 26px;"><span class="bjh-li" style="max-width: 100%; display: list-item; margin-bottom: 11px;"><span class="bjh-p" style="max-width: 100%;">若在执行pip命令时出现"pip不是内部或外部命令"的错误,可能是pip没有正确添加到系统的环境变量中。<span class="bjh-strong" style="max-width: 100%; font-weight: 700;">解决方法</span>是使用pip的完整路径运行命令,或重新安装Python并勾选"Add Python to PATH"选项。</span></span><span class="bjh-li" style="max-width: 100%; display: list-item; margin-bottom: 0px;"><span class="bjh-p" style="max-width: 100%;">某些情况下,使用pip安装库时可能会出现权限问题,解决方法是将命令行或终端以管理员权限运行。</span></span></span><span class="bjh-h3" style="overflow-y: auto; max-width: 100%; display: inline-block; position: relative; padding-left: 9px; font-weight: 700; color: rgb(0, 0, 0);">结论</span></div><div class="dpu8C _2kCxD " style="max-width: 100%; overflow-x: visible; line-height: 30px; margin-top: 24px;"><span class="bjh-p" style="overflow-y: auto; max-width: 100%;">通过本文的详细介绍,我们了解了在Python中安装和使用pip命令的方法。pip作为Python的包管理工具,可以方便地安装、卸载和管理第三方库,为我们的Python编程提供了很大的便利。</span></div></div>