pip3在Ubuntu下的安装、升级、卸载

这篇具有很好参考价值的文章主要介绍了pip3在Ubuntu下的安装、升级、卸载。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

一、参考资料

pip 常用命令
pip 官方文档

二、安装pip包

如何在 Ubuntu 20.04 上安装 Python Pip - 知乎 (zhihu.com)

1. 离线安装

Installation
pip下载地址

1.1 为 Python 3 安装 pip

方式一
# 下载get-pip.py脚本
wget https://bootstrap.pypa.io/pip/3.6/get-pip.py
或者
# 下载最新版本
curl https://bootstrap.pypa.io/get-pip.py --output get-pip.py
 
# 为 Python 3 安装 pip
sudo python3 get-pip.py

ubuntu安装pip3,运维,linux,pip

方式二
# 下载tar.gz安装包
tar -xzvf pip-22.3.1.tar.gz
 
# 解压 
tar -xzvf pip-22.3.1.tar.gz
 
# 安装pip3 
sudo python3 setup.py install
 
# 检查pip3版本号    
pip –version

1.2 为 Python 2 安装 pip

# 下载get-pip.py脚本
curl https://bootstrap.pypa.io/get-pip.py --output get-pip.py

# 为 Python 2 安装 pip
sudo python2 get-pip.py

2. 在线安装

2.1 为 Python 3 安装 pip

# 更新软件包索引,安装pip
sudo apt-get update
sudo apt-get install python3-pip

# 查看pip3版本
pip3 --version

2.2 为 Python 2 安装 pip

# 启用 universe 源仓库
sudo add-apt-repository universe

# 更新软件包索引,安装Python2
sudo apt update 
sudo apt install python2

# 查看pip版本
pip2 --version

3. 升级pip

# 升级pip,--upgrade 可简写为 -U
sudo pip3 install --upgrade pip
或者
python3 -m pip install --upgrade pip

4. 卸载pip3

# 卸载pip3    
sudo apt-get remove python-pip3
或者
sudo apt-get remove python3-pip

5. pip安装方式对比

5.1 从PyPI安装

# From PyPI:
pip install dm-tree

5.2 从github安装

# Directly from github using pip:
pip install git+git://github.com/deepmind/tree.git

5.3 从源码安装

# Build from source:
# 下载地址:https://pypi.org/project/Tree
tar -xvzf Tree-0.2.4.tar.gz

python setup.py install

5.4 从whl安装

# 下载whl安装包
numpy-1.24.1-pp38-pypy38_pp73-win_amd64.whl

# 解压,进入文件夹
pip install numpy-1.24.1-pp38-pypy38_pp73-win_amd64.whl

三、pip常用命令

1. --user安装

# 代表进行全局安装,安装后全局可用。如果是信任的安装包可用使用该命令进行安装。
sudo pip3 install

# 代表仅该用户的安装,安装后仅该用户可用。处于安全考虑,建议使用该命令进行安装。
pip3 install --user 

2. pip install

# 用pip3安装包
pip3 install 包名

# 非root用户安装,安装到用户目录,包会安装到用户目录下
pip3 install 包名 --user

# 离线安装
pip3 install ./downloads/SomeProject-1.0.4.tar.gz
# 安装最新版本
pip3 install 包名  
# 安装指定版本
pip3 install 包名==1.8 
# 安装最小版本
pip3 install 包名>=1.8  

# 安装 PIP 包,建议增加 --no-cache 参数禁用本地缓存
pip install flask --no-cache

# 忽略包是否安装,都重新安装
pip3 install 包名 --ignore-installed 

# 设置超时连接时间为60s,默认为15s
pip3 install 包名 --timeout=60 

# 安装到指定位置
pip3 install 包名 -t /PATH/TO/×××

# 每次调用包的时候,需要加载该路径下的包
import sys
sys.path.append('/PATH/TO/×××')
# freeze输出安装包
# 输出到requirements.txt文件中
pip3 freeze > requirements.txt 

# 通过requirements文件批量安装
pip3 install -r requirements.txt

# 强烈推荐:批量安装跳过无法安装的包继续运行
while read requirement; do sudo pip3 install $requirement -i https://mirrors.aliyun.com/pypi/simple/; done < requirement.txt
# 更新版本,--upgrade 可简写为 -U
pip3 install --upgrade 包名

# 如果更新失败,尝试添加 --ignore-installed
pip3 install --upgrade --ignore-installed 包名

# 卸载包
pip3 uninstall 包名

镜像源

阿里云	        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/
Pypi官方       https://pypi.org/simple/
# 使用国内镜像安装,--index-url 简写为 -i
pip3 install 包名 -i https://pypi.tuna.tsinghua.edu.cn/simple/
或者
sudo pip3 install --index-url https://mirrors.aliyun.com/pypi/simple tensorflow-gpu==1.14

# 使用清华源升级pip
sudo pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple pip -U

3. 设置pip与python版本

# 1. 查看pip2路径
which pip

输出:
/usr/bin/pip

# 2. 查看pip3路径
which pip3

输出:
/usr/bin/pip3

# 3. 查看python2的路径
which python2

输出:
/usr/bin/python2

# 4. 查看python3的路径
which python3

输出:
/usr/bin/python3
# 5. 修改pip2对应python2
sudo gedit /usr/bin/pip

# 第一行,改为:
#!/usr/bin/python2

# 6. 修改pip3对应python3
sudo gedit /usr/bin/pip3

# 第一行,改为:
#!/usr/bin/python3

4. 其他指令

# 从 git 安装
pip3 install -e git+https://git.repo/some_pkg.git#egg=SomeProject  

# 从 mercurial 安装
pip3 install -e hg+https://hg.repo/some_pkg#egg=SomeProject  

# 从 svn 安装
pip3 install -e svn+svn://svn.repo/some_pkg/trunk/#egg=SomeProject   

# 安装某一分支 branch
pip3 install -e git+https://git.repo/some_pkg.git@feature#egg=SomeProject  
# show显示包的版本信息,依赖关系,安装位置等信息
pip3 show 包名 

# search根据关键字搜索包
# 截止到2021年10月18日,官网暂时关掉了该功能
https://status.python.org/

# 搜索关键字,会列出所有与关键字相关的软件包
pip3 search 关键字  

四、FAQ

Q: pip内部错误

python pip 错误 ModuleNotFoundError: No module named pip._internal 解决办法

pip错误 ImportError: No module named _internal Traceback (most recent call last):

    File "/home/ubuntu/.local/bin/pip", line 7, in <module> from pip._internal import main ImportError: No module named _internal
解决方案:
强制重新安装pip3

wget https://bootstrap.pypa.io/get-pip.py  --no-check-certificate
     	
sudo python3 get-pip.py --force-reinstall

Q:ImportError: No module named pip._internal.cli.main

yoyo@yoyo:/opt/ros/kinetic/lib$ pip list
Traceback (most recent call last):
  File "/home/yoyo/.local/bin/pip", line 5, in <module>
    from pip._internal.cli.main import main
ImportError: No module named pip._internal.cli.main

Q: pip下载超时

PIP安装超时的解决方案

Q: pip2安装包失败

python2 中使用pip2 install package_name的时候报错:AttributeError: ‘int‘ object has no attribute ‘endswith‘

Traceback (most recent call last):
  File "/usr/share/python-wheels/pip-9.0.1-py2.py3-none-any.whl/pip/basecommand.py", line 215, in main
    status = self.run(options, args)
  File "/usr/share/python-wheels/pip-9.0.1-py2.py3-none-any.whl/pip/commands/install.py", line 290, in run
    with self._build_session(options) as session:
  File "/usr/share/python-wheels/pip-9.0.1-py2.py3-none-any.whl/pip/basecommand.py", line 69, in _build_session
    if options.cache_dir else None
  File "/media/mydisk/MyDocuments/PyProjects/automl/efficientdet/venv/lib/python2.7/posixpath.py", line 70, in join
    elif path == '' or path.endswith('/'):
AttributeError: 'int' object has no attribute 'endswith'
解决办法:
在使用pip2安装命令之前设置:PIP_NO_CACHE_DIR=off

比如:
PIP_NO_CACHE_DIR=off pip2 install -U pip2

Q: pip版本太低了

TypeError: expected str, bytes or os.PathLike object, not int(解决方法)

Exception:
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/pip/basecommand.py", line 215, in main
    status = self.run(options, args)
  File "/usr/lib/python3/dist-packages/pip/commands/install.py", line 290, in run
    with self._build_session(options) as session:
  File "/usr/lib/python3/dist-packages/pip/basecommand.py", line 69, in _build_session
    if options.cache_dir else None
  File "/usr/lib/python3.6/posixpath.py", line 80, in join
    a = os.fspath(a)
TypeError: expected str, bytes or os.PathLike object, not int
解决办法:
升级pip

对于python2:
curl https://bootstrap.pypa.io/get-pip.py | python2 -

对于python3:
curl https://bootstrap.pypa.io/get-pip.py | python3 -
tx2@tx2:/media/mydisk/MyDocuments/PyProjects/automl/efficientdet$ curl https://bootstrap.pypa.io/get-pip.py | python3 -
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 2108k  100 2108k    0     0  36367      0  0:00:59  0:00:59 --:--:--  377k
Defaulting to user installation because normal site-packages is not writeable
Looking in indexes: https://pypi.org/simple, https://pypi.ngc.nvidia.com
Collecting pip
  Downloading pip-21.3-py3-none-any.whl (1.7 MB)
     |████████████████████████████████| 1.7 MB 9.5 kB/s            
Installing collected packages: pip
  WARNING: The scripts pip, pip3 and pip3.6 are installed in '/home/tx2/.local/bin' which is not on PATH.
  Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
Successfully installed pip-21.3

Q: pip警告

WARNING: pip is being invoked by an old script wrapper.

TypeError: expected str, bytes or os.PathLike object, not int(解决方法)

Python pip的安装及卸载

tx2@tx2:/media/mydisk/MyDocuments/PyProjects/automl/efficientdet$ pip --version
WARNING: pip is being invoked by an old script wrapper. This will fail in a future version of pip.
Please see https://github.com/pypa/pip/issues/5599 for advice on fixing the underlying issue.
To avoid this problem you can invoke Python with '-m pip' instead of running pip directly.
pip 21.3 from /home/tx2/.local/lib/python3.6/site-packages/pip (python 3.6)
错误原因:
setuptools和pip的版本不对应,所以升级一下pip就可以了
pip和python版本不匹配

解决办法:
方法一:
在pip指令前,添加 python3 -m
比如: python3 -m pip install numpy

查看pip版本以及安装路径
python3 -m pip --version
pip 21.3 from /home/tx2/.local/lib/python3.6/site-packages/pip (python 3.6)

卸载pip
python3 -m pip uninstall pip

卸载pip3
sudo apt-get remove python3-pip
sudo apt autoremove


重装python2对应的pip
curl https://bootstrap.pypa.io/get-pip.py | python -

或者
wget https://bootstrap.pypa.io/get-pip.py
python get-pip.py

重装python3对应的pip3
curl https://bootstrap.pypa.io/get-pip.py | python3 -

或者
wget https://bootstrap.pypa.io/get-pip.py
修改 get-pip.py 第一行,修改为 #!/usr/bin/env python3
python3 get-pip.py

更新setuptools
pip3 install --upgrade setuptools

升级pip
python3 -m pip install -U pip

安装包
python -m pip 包名
tx2@tx2:/media/mydisk/MyDocuments/PyProjects/automl/efficientdet$  curl https://bootstrap.pypa.io/get-pip.py | python3 -
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100 2108k  100 2108k    0     0   381k      0  0:00:05  0:00:05 --:--:--  494k
Defaulting to user installation because normal site-packages is not writeable
Looking in indexes: https://pypi.org/simple, https://pypi.ngc.nvidia.com
Collecting pip
  Downloading pip-21.3-py3-none-any.whl (1.7 MB)
     |████████████████████████████████| 1.7 MB 56 kB/s             
Installing collected packages: pip
Successfully installed pip-21.3

Q: 更新pip错误

解决python pip 安装报语法错误sys.stderr.write(f“ERROR: {exc}“)
pip documentation v21.3.1

yoyo@yoyo:~/MyDocuments/PyProjects/Auto-Depth$ pip3 --version
Traceback (most recent call last):
  File "/home/yoyo/.local/bin/pip3", line 7, in <module>
    from pip._internal.cli.main import main
  File "/home/yoyo/.local/lib/python3.5/site-packages/pip/_internal/cli/main.py", line 57
    sys.stderr.write(f"ERROR: {exc}")
                                   ^
SyntaxError: invalid syntax
错误原因:
python版本与pip版本不匹配
博主当前python版本为3.5,更新pip版本为21.3.1,导致python版本与pip版本不匹配
Pip21.0.1版本在2020年1月停止支持Python2,在2020年3月停止支持Python3.5

方法一:
更新python版本
方法二:
wget https://bootstrap.pypa.io/pip/3.5/get-pip.py
或者(如果上面的指令失败)
wget https://bootstrap.pypa.io/3.5/get-pip.py

python3 get-pip.py

Q:ERROR: This script does not work on Python 3.5 The minimum supported Python version is 3.6.

yoyo@yoyo:~/下载$ python3 get-pip.py 
ERROR: This script does not work on Python 3.5 The minimum supported Python version is 3.6. Please use https://bootstrap.pypa.io/pip/3.5/get-pip.py instead.
错误原因:
python版本与pip版本不匹配

解决办法:
安装对应版本的pip
wget https://bootstrap.pypa.io/pip/3.5/get-pip.py --no-check-certificate
python3 get-pip.py --force-reinstall

Q: Defaulting to user installation because normal site-packages is not writeable

Defaulting to user installation because normal site-packages is not writeable

Q: pip镜像源地址错误

pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.
Could not fetch URL https://mirrors.aliyun.com/pypi/simple/pip/: There was a problem confirming the ssl certificate: HTTPSConnectionPool(host='mirrors.aliyun.com', port=443): Max retries exceeded with url: /pypi/simple/pip/ (Caused by SSLError("Can't connect to HTTPS URL because the SSL module is not available.",)) - skipping
错误原因:
pip镜像源地址错误

解决办法:
https改为http

sudo gedit ~/.pip/pip.conf

[global]
timeout = 6000
index-url = https://mirrors.aliyun.com/pypi/simple/
trusted-host = mirrors.aliyun.com
改为
[global]
timeout = 6000
index-url = http://mirrors.aliyun.com/pypi/simple/
trusted-host = mirrors.aliyun.com

Q: pip安装找不到包

(pt2pb-tf114) PS F:\MyDocumentes\YOYOFile\Seeking\SeekingCode\deep_learn_frame-pyt-seg> pip install tensorflow==1.14 -i https://mirrors.aliyun.com/pypi/simple/
Looking in indexes: https://mirrors.aliyun.com/pypi/simple/
ERROR: Could not find a version that satisfies the requirement tensorflow==1.14 (from versions: 2.5.0, 2.5.1, 2.5.2, 2.5.3, 2.6.0rc0, 2.6.0rc1, 2.6.0rc2, 2.6.0, 2.6.1, 2.6.2, 2.6.3, 2
.7.0rc0, 2.7.0rc1, 2.7.0, 2.7.1, 2.8.0rc0, 2.8.0rc1, 2.8.0, 2.9.0rc0, 2.9.0rc1)
ERROR: No matching distribution found for tensorflow==1.14
错误原因:
python3.9没有与之匹配的tensorflow版本。
博主当前的python版本是python3.9.12,支持tensorflow2.5+以上的版本。可以通过 https://mirrors.aliyun.com/pypi/simple/ 镜像网站查找python3.9所支持的 tensorflow 版本。

方法一:
降低python版本

方法二:
更换高版本的 <tensorflow

Q: GPG error 缺少公钥

W: GPG error: http://mirrors.ustc.edu.cn stable InRelease: The following signatures couldn't be verified because the public key is not available: NO_PUBKEY 648ACFD622F3D138 NO_PUBKEY 0E98404D386FA1D9 NO_PUBKEY 605C66F00D6C9793
W: GPG error: http://mirrors.ustc.edu.cn stable-updates InRelease: The following signatures couldn't be verified because the public key is not available: NO_PUBKEY 648ACFD622F3D138 NO_PUBKEY 0E98404D386FA1D9

The following signatures couldn‘t be verified because the public key is not available: NO_PUBKEY 问题
https://blog.csdn.net/uknow0904/article/details/108841550
错误原因:
缺少公钥

解决办法:
按照报错提示,添加公钥

sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys <公钥>

Q:pip command not found

Linux pip command not found【已安装anaconda】文章来源地址https://www.toymoban.com/news/detail-822045.html

# 打开配置文件
vim ~/.bashrc

# 添加配置
alias pip = '/home/user/anaconda3/bin/pip3'

# 更新配置
source ~/.bashrc

到了这里,关于pip3在Ubuntu下的安装、升级、卸载的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 通过yum安装python3.9和pip3

    你好! 要使用 yum 安装 Python 3.9 和 pip3,需要执行以下步骤: 首先,确保你的系统上已经安装了 yum。如果没有,请参考如何在 CentOS 上安装 yum。 然后,使用 yum 命令安装 Python 3.9。 接下来,使用 yum 命令安装 pip3。 最后,检查是否已成功安装 Python 3.9 和 pip3。 如果一切顺利,

    2024年02月11日
    浏览(46)
  • pip3 安装包时出现大堆错误,可以尝试安装较低版本

    用Pip3安装各种包时,有时会出现一大堆的编译语法错误, 这可能跟包的版本太新、与其他软件包版本不一致有关。 可以尝试安装较低版本,本人有几次这样安装通过。

    2024年01月19日
    浏览(44)
  • Ubuntu如何卸载安装包

    本文介绍如何在Ubuntu环境下如何彻底卸载原来安装的应用程序。 注意 :以上package_name为要卸载的安装包。如qv4l2:

    2024年02月12日
    浏览(35)
  • ubuntu安装、卸载docker

    一、卸载docker 1. 删除docker相关软件,及其安装时自动安装的所有包          sudo apt-get autoremove docker docker-ce docker-engine  docker.io  containerd runc 2. 删除docker其他没有没有卸载         dpkg -l | grep docker 3.卸载没有删除的docker相关插件         sudo apt-get autoremove docker-ce-

    2024年02月07日
    浏览(35)
  • ubuntu 安装/卸载/查找 boost

    参考: https://blog.csdn.net/guoguangwu/article/details/89245243 https://blog.csdn.net/qq_39885372/article/details/104397883 如果没有查到,也不表示没有安装,有可能是手动下载安装包安装的。检查办法是 如果都有查到,说明没有安装。 可以先安装一个: 然后查看可以安装的版本: 例如,我需要安

    2023年04月26日
    浏览(27)
  • cuda 安装和卸载 (Ubuntu 2204)

    cuda需要Nvidia显卡或计算卡,AMD或intel显卡不行(但是也有套他们的标准) 就算是亮机卡也可使用,比如GT710这种 建议使用ubuntu来装,因为cuda就是在这个平台上开发的,当然别的linux系统也行 以下操作在ubuntu server 2204、debian12、debian11中都操作过,如果没有安装linux系统,可以

    2024年02月04日
    浏览(30)
  • VMware安装及其Ubuntu在虚拟中的安装与卸载(含Ubuntu系统配置)

    1.去vmware官网下载; 2.或者直接在搜索引擎搜索,网上有很多分享的虚拟机安装包。 1.下载文件:安装之后是这样的界面。 2.如果你点击下一步出现了这个页面的话,那就证明你电脑里面已经有VMware了,可以直接使用,如果想重新安装,直接删除就好,再重新打开安装程序继

    2024年02月08日
    浏览(42)
  • Ubuntu 下安装软件,卸载,查看已经安装的软件

    参考网址:http://wiki.ubuntu.org.cn/UbuntuSkills 一般的安装程序用三种:  .deb 和.rpm 这两种安装文件  .bundle 这是二进制的安装文件 而 tar.gz 这类的只是压缩包(相当于 .rar,.zip 压缩包一样),如果此类文件是程序的话,得先解压。 用户手动选择安装目录是不必要的。这一套系统更了

    2024年02月16日
    浏览(35)
  • ubuntu下docker卸载和重新安装

    卸载:步骤一:停止Docker服务 首先,我们需要停止正在运行的Docker服务。打开终端,执行以下命令: sudo systemctl stop docker 步骤二:删除Docker安装包 接下来,我们需要删除已经安装的Docker软件包。执行以下命令: sudo apt-get purge docker-ce docker-ce-cli containerd.io 步骤三:删除Docke

    2024年01月25日
    浏览(30)
  • ubuntu中用apt命令安装、卸载软件

    命令 :apt install … 如:apt install influxdb-client 命令:apt purge … 如:apt purge influxdb-client 补充:删除软件包,同时删除了相应依赖软件包`。

    2024年02月11日
    浏览(38)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包