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

参考资料
pip 常用命令
pip 官方文档

一、常用命令
1. 在线安装
>> sudo apt-get install python3-pip
 
1.1 离线安装
 
# 下载get-pip.py脚本
wget https://bootstrap.pypa.io/pip/3.6/get-pip.py
或者
curl https://bootstrap.pypa.io/get-pip.py | python3 -
 
# 离线安装
python3 get-pip.py
 
2. 升级pip
>> sudo pip3 install --upgrade pip
# --upgrade 可简写为 -U
 
3. 卸载
>> sudo apt-get remove python3-pip
 
4. 用pip3安装包
>> pip3 install 包名
 
4.1 更新版本
>> pip3 install --upgrade 包名
# --upgrade 可简写为 -U
 
4.2 如果更新失败,尝试添加 --ignore-installed
>> pip3 install --upgrade --ignore-installed 包名
 
4.3 安装指定版本
>> pip3 install 包名       # 最新版本
>> pip3 install 包名==1.8  # 指定版本
>> pip3 install 包名>=1.8  # 最小版本
 
4.4 卸载包
>> pip3 uninstall 包名
 
4.5 安装到指定位置
>> pip3 install 包名 -t /PATH/TO/×××
 
每次调用包的时候,需要加载该路径下的包
import sys
sys.path.append('/PATH/TO/×××')
 
4.6 安装到用户目录
>> pip3 install 包名 --user # 包会安装到用户目录下
 
4.7 忽略是否安装
>> pip3 install 包名 --ignore-installed # 忽略包是否安装,都重新安装
 
4.8 设置超时
>> pip3 install 包名 --timeout=60 # 设置超时连接时间为60s,默认为15s
 
 
5. 使用国内镜像安装
>> 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 # 包名
# --index-url 简写为 -i
 
使用清华源升级pip
>> sudo pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple pip -U
 
阿里云            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/
 
6. 通过requirements文件批量安装
>> pip3 install -r requirements.txt
 
强烈推荐:批量安装跳过无法安装的包继续运行
while read requirement; do sudo pip3 install $requirement -i https://mirrors.aliyun.com/pypi/simple/; done < requirement.txt
 
 
7. 从VCS安装
>> pip3 install -e git+https://git.repo/some_pkg.git#egg=SomeProject          # 从 git 安装
>> pip3 install -e hg+https://hg.repo/some_pkg#egg=SomeProject                # 从 mercurial 安装
>> pip3 install -e svn+svn://svn.repo/some_pkg/trunk/#egg=SomeProject         # 从 svn 安装
>> pip3 install -e git+https://git.repo/some_pkg.git@feature#egg=SomeProject  # 安装某一分支 branch
 
8. 本地安装
>> pip3 install ./downloads/SomeProject-1.0.4.tar.gz
 
9. freeze输出安装包
>> pip3 freeze
>> pip3 freeze > /PATH/TO/requirements.txt # 输出到requirements.txt文件中
 
10. show显示包所在的目录及信息
>> pip3 show 包名
# 可查看安装包的版本,安装位置,依赖的安装包以及被被那些安装包依赖
 
11. search根据关键字搜索包
截止到2021年10月18日,官网暂时关掉了该功能
https://status.python.org/
 
>> pip3 search 关键字  # 会列出所有与关键字相关的软件包
二、gz压缩包下载安装
1. 卸载pip3    
>> sudo apt-get remove python-pip3
 
2. 源码安装pip3
下载地址:https://pypi.python.org/pypi/pip
 
 
3. 打开终端,解压 
>> sudo tar -xzvf pip-9.0.1.tar.gz
 
4. 安装pip3:   
>> sudo python3 setup.py install --user
或 sudo python3 setup.py build
 
5. 检查pip3版本号:    
>> pip –version
三、.whl文件安装
1. 解压,进入文件夹
>> python3  setup.py  install
 
或 pip3  install  somewhat.whl --user
四、对比三种安装方式
# From PyPI:
pip install dm-tree
 
# Directly from github using pip:
pip install git+git://github.com/deepmind/tree.git
 
# Build from source:
python setup.py install
五、可能出现的问题
Q: pip内部错误
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: pip下载超时
PIP安装超时的解决方案

Q: 设置pip与python版本
# 1. 查看pip路径
which pip
 
输出:
/usr/bin/pip
 
# 2. 查看pip3路径
which pip
 
输出:
/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
Q: pip2安装包失败
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'
python2 中使用pip2 install package_name的时候报错:AttributeError: ‘int‘ object has no attribute ‘endswith‘

# 解决办法:
在使用pip2安装命令之前设置:PIP_NO_CACHE_DIR=off
 
比如:
PIP_NO_CACHE_DIR=off pip2 install -U pip2
Q: pip版本太低了
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
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
 
卸载pip
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错误
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版本
方法二:
[解决python pip 安装报语法错误sys.stderr.write(f“ERROR: {exc}“)](https://blog.csdn.net/hardhard123/article/details/113251805)
 
[pip documentation v21.3.1](https://pip.pypa.io/en/stable/installation/)
wget https://bootstrap.pypa.io/pip/3.5/get-pip.py
python3 get-pip.py
或者(如果上面的指令失败)
wget https://bootstrap.pypa.io/3.5/get-pip.py
python3 get-pip.py
 
方法二可能出现的问题:
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版本
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 <公钥>
————————————————
版权声明:本文为CSDN博主「花花少年」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/m0_37605642/article/details/93464105

猜你喜欢

转载自blog.csdn.net/pj_wxyjxy/article/details/126222399