pip3 常见用法总结

换源

第一件事当然是换源 ~

pip 国内的一些镜像

阿里云 http://mirrors.aliyun.com/pypi/simple/
中国科技大学 https://pypi.mirrors.ustc.edu.cn/simple/
豆瓣(douban) http://pypi.douban.com/simple/
清华大学 https://pypi.tuna.tsinghua.edu.cn/simple/
中国科学技术大学 http://pypi.mirrors.ustc.edu.cn/simple/

修改源方法:

临时使用:
可以在使用pip的时候在后面加上-i参数,指定pip源

eg: pip install scrapy -i https://pypi.tuna.tsinghua.edu.cn/simple

永久修改:

LInux系统
修改 ~/.pip/pip.conf (没有就创建一个), 内容如下:

[global]
index-url = https://pypi.tuna.tsinghua.edu.cn/simple

Windows:
win+R 打开用户目录%HOMEPATH%,在此目录下创建 pip 文件夹,在 pip 目录下创建 pip.ini 文件, 内容如下:

[global]

timeout = 6000

index-url = https://pypi.tuna.tsinghua.edu.cn/simple

trusted-host = pypi.tuna.tsinghua.edu.cn
conda 换源

添加清华的镜像源

conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/msys2/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/conda-forge/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
conda config --set show_channel_urls yes
conda - -help // 找到配置文件.condarc 的路径 ,将上述显示的配置信息中 - defaults 所在行删除保存即可

查看源:

conda config --show channels

换回默认源

conda config --remove-key channels
用法总览
pip3 --help
Usage:   
  pip <command> [options]

Commands:
  install                     Install packages.
  download                    Download packages.
  uninstall                   Uninstall packages.
  freeze                      Output installed packages in requirements format.
  list                        List installed packages.
  show                        Show information about installed packages.
  check                       Verify installed packages have compatible dependencies.
  search                      Search PyPI for packages.
  wheel                       Build wheels from your requirements.
  hash                        Compute hashes of package archives.
  completion                  A helper command used for command completion.
  help                        Show help for commands.

General Options:
  -h, --help                  Show help.
  --isolated                  Run pip in an isolated mode, ignoring environment variables and user
                              configuration.
  -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.
  --proxy <proxy>             Specify a proxy in the form [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 as trusted, even though it does not have valid or any HTTPS.
  --cert <path>               Path to alternate CA bundle.
  --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.

一、安装 & 移除
安装指定包的最新版
pip3 install pkg_name
安装制定包的指定版本
pip3 install pkg_name==version

$ pip3 install django==2.0.5
移除指定包
pip3 uninstall pkg_name
在 PyPI 上模糊查找相关包
pip3 search pkg_name
二、查看相关
查看所有已安装的包
pip3 list

形如:
amqp (1.4.9)
anyjson (0.3.3)
asn1crypto (0.24.0)
查看指定包的相关信息
pip3 show pkg_name

$ pip3 show django
Name: Django
Version: 2.1.4
Summary: A high-level Python Web framework that encourages rapid development and clean, pragmatic design.
Home-page: https://www.djangoproject.com/
Author: Django Software Foundation
Author-email: foundation@djangoproject.com
License: BSD
Location: /home/ezreal/.local/lib/python3.6/site-packages
Requires: pytz
三、更新相关
更新 pip

在 Anaconda prompt 环境下

conda upgrade pip

普通环境

python -m pip install --upgrade pip
更新包

检查哪些包需要更新

pip list --outdated

更新指定包

pip install --upgrade pkg_name
四、freeze 的使用
列出所有已安装包
pip3 freeze

形如:
vine==1.1.4
virtualenv==16.1.0
youtube-dl==2018.9.10
生成 requirements.txt 文件
  • 将当前 Python 环境下所有类库导出生成为 requirements.txt
pip3 freeze > requirements.txt
  • 将特定项目使用的类库导出生成为 requirements.txt(其中.为当前文件夹下的项目,可改成你项目的路径)
# 需要安装第三方库
pip3 install pipreqs

# .表示当前目录
pipreqs . --encoding=utf8
  • 安装 requirements.txt 依赖
pip3 install -r requirements.txt
五、遇到的异常及解决
PermissionError: [WinError 5] 拒绝访问

在这里插入图片描述
解决:加上--user参数

pip install --user --upgrade tensorflow-gpu
REFERENCES

[1] PIP 更换国内安装源:https://blog.youkuaiyun.com/yuzaipiaofei/article/details/80891108

03-11
<think>好的,我现在需要帮助用户了解如何使用pip3进行Python包管理。根据用户的问题,他们可能是一个刚开始学习Python的新手,想要掌握基本的包管理操作。首先,我要回忆一下pip3的基本用法,确保自己记得正确。 首先,用户提到的是pip3,所以应该区分pippip3。通常,pip3是Python 3版本的包管理工具,而pip可能指向Python 2,不过现在大多数情况下两者可能已经合并了。但为了准确,我应该明确说明使用pip3来管理Python 3的包。 接下来,用户的问题包括安装和管理包,所以我需要分步骤说明。安装包是最基本的需求,应该放在前面。记得要提到如何安装特定版本,以及从requirements文件安装,这部分在引用中也有提到,比如引用[2]和引用[3]中的内容。需要确保用户知道这些方法。 然后,管理包的部分包括查看已安装的包、升级包、卸载包,以及管理依赖。这里需要给出具体的命令示例,比如pip3 list,pip3 show,pip3 install --upgrade,pip3 uninstall等。这些命令在引用中都有涉及,尤其是引用[3]详细说明了这些操作,所以需要正确引用。 用户可能遇到的问题包括权限问题,比如在Linux或macOS上需要使用sudo,或者如何在虚拟环境中使用pip3。这部分需要提醒用户,避免他们遇到权限错误。另外,虚拟环境是一个重要的概念,可以稍微提及,但可能不需要深入,除非用户进一步询问。 还有,用户可能需要知道如何生成requirements.txt文件,这在团队协作或部署时非常有用。引用[2]和引用[3]都提到了使用requirements文件来管理依赖,所以这部分需要详细说明,包括如何生成和使用这个文件。 另外,用户可能对PyPI不太了解,可以简单解释一下PyPI的作用,如引用[2]提到的,PyPI是Python包的仓库,pip3从这里下载包。但根据问题,用户可能更关注操作而不是概念,所以可能不需要深入,但可以提及。 需要检查是否有遗漏的步骤,比如如何检查pip3的版本,或者如何确保pip3本身是最新的。引用[1]提到使用ensurepip来安装pip,但用户可能已经安装了pip3,所以可能只需要提醒升级pip3的命令。 最后,要确保回答结构清晰,分步骤说明,每个步骤都有对应的命令示例,并且引用相关的参考资料。例如,在讲解安装包时,引用[3]提到使用pip install,所以需要正确标注引用。同时,用户可能需要常见问题解答,比如镜像源的使用,这在安装速度慢时很有帮助,可以加入。 总结一下,回答的结构应该是: 1. 安装包(基本命令、指定版本、从文件安装) 2. 管理已安装的包(查看、升级、卸载) 3. 依赖管理(生成requirements.txt) 4. 其他技巧(使用镜像源、虚拟环境) 5. 常见问题(权限问题) 需要确保每个部分都有对应的命令示例,并且语言简洁明了,适合新手理解。同时,检查所有命令是否正确,比如pip3 install package_name是否正确,是否需要使用sudo等。还要注意中文的流畅性,避免直译导致的理解困难。 可能还需要提醒用户注意Python环境和pip3的对应关系,避免在不同环境中混淆。例如,如果用户使用了虚拟环境,需要在激活虚拟环境后使用pip3,否则会影响全局环境。这部分虽然稍微高级,但对于管理多个项目很重要,可以简要提及。 最后,生成的相关问题需要与pip3的使用相关,比如如何批量安装、如何解决依赖冲突、如何发布自己的包等,这些可以帮助用户进一步探索。</think>### 如何使用pip3安装和管理Python包 pip3是Python 3的包管理工具,用于安装、升级、卸载第三方库及管理依赖。以下是详细操作指南: --- #### 一、安装Python包 1. **安装单个包** 命令格式:`pip3 install <包名>` 示例:安装`requests`库 ```bash pip3 install requests ``` 2. **指定版本安装** 若需安装特定版本,使用`==`符号: ```bash pip3 install numpy==1.21.0 ``` 3. **从requirements文件批量安装** 通过`requirements.txt`文件管理项目依赖: ```bash pip3 install -r requirements.txt ``` 引用[^2][^3] --- #### 二、管理已安装的包 1. **查看已安装的包** - 列出所有包:`pip3 list` - 查看某个包的详细信息:`pip3 show <包名>` 2. **升级包** ```bash pip3 install --upgrade <包名> ``` 3. **卸载包** ```bash pip3 uninstall <包名> ``` 引用[^3] --- #### 三、依赖管理 1. **生成requirements文件** 导出当前环境的所有依赖: ```bash pip3 freeze > requirements.txt ``` 此文件可用于共享或复现环境。 --- #### 四、其他实用技巧 1. **使用国内镜像加速下载** 临时指定镜像源(例如清华源): ```bash pip3 install <包名> -i https://pypi.tuna.tsinghua.edu.cn/simple ``` 2. **在虚拟环境中使用pip3** 推荐使用`venv`或`virtualenv`隔离项目环境,避免全局安装冲突。 --- #### 五、常见问题 - **权限问题**:在Linux/macOS中,若提示权限不足,可添加`--user`参数或在命令前加`sudo`。 - **pip3未找到**:需确保Python 3已正确安装,并通过`python3 -m ensurepip --upgrade`修复[^1]。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值