Linux - 在Ubuntu中安装Python和PIP

news2024/9/23 5:14:46
1, 安装Python
安装Python2:
sudo apt update
sudo apt install python2
安装Python3:
sudo apt update
sudo apt install python3
安装完成后检查Python版本:
$ python2 -V
$ python3 -V
Python 3.10.12
在调用pip或python时,也可以加上具体版本号,比如:
$ python2 -V
$ python3 -V
$ python3.8 -V
$ pip3 --version
$ pip3.10 --version
备注:
在使用apt安装python时,需要sudo,但使用pip时,不需要使用sudo,可能会有不好的结果。
2, 安装PIP工具
pip 是 Python 的软件包安装程序(版本 3.4 和 2.7.9 之后, 它默认包含在 Python 二进制安装程序中),允许用户轻松安装和管理不属于标准 Python 库的附加库和依赖项。使用 pip,您可以从 Python 软件包索引(PyPI)中下载软件包并保持更新,因此它对 Python 开发至关重要。它简化了将第三方模块集成到项目中的过程,最终提升了您的编程体验。 
pip is the package installer for Python (after version 3.4 and 2.7.9, it is included by default with the Python binary installers ) , allowing users to easily install and manage additional libraries and dependencies that are not part of the standard Python library. With pip, you can download packages from the Python Package Index (PyPI) and keep them updated, making it essential for Python development. It simplifies the process of integrating third-party modules into your projects, ultimately enhancing your programming experience.
The standard packaging tools are all designed to be used from the command line.
Python可以安装的包,可以在PyPI网站查询: PyPI · The Python Package Index
PyPI是Python软件包索引( Python Package index ),Python模块的存储库(a public repository of open source licensed packages)。  PyPI是由Python软件基金会( Python Software Foundation)主持的。
Python Packaging Authority: Python Packaging Authority — PyPA documentation
安装python2的pip:
sudo apt install python-pip
安装python3的pip:
sudo apt install python3-pip
验证安装,安装完成后,可以使用 pip 检查版本:
Verify Installation, a fter installation, you can check the version of pip using:
pip3 --version # For Python 3 
pip --version # For Python 2
 也可以用Python命令来调用pip模块来检查版本:
python3 -m pip --version
正常情况下,python安装时自带pip,所以也可以使用下面命令修复:
python -m ensurepip --default-pip
注意:
需要注意的是,这里的 “包 ”一词是指要安装的软件包(即发行版的同义词)。它并不是指在 Python 源代码中导入的那种包(即模块容器)。在 Python 社区中,通常使用 “包 ”来指代发行版。使用 “发行版 ”一词通常并不可取,因为它很容易与 Linux 发行版或其他更大的软件发行版(如 Python 本身)相混淆。
It’s important to note that the term “package” in this context is being used to describe a bundle of software to be installed (i.e. as a synonym for a distribution). It does not refer to the kind of package that you import in your Python source code (i.e. a container of modules). It is common in the Python community to refer to a distribution using the term “package”. Using the term “distribution” is often not preferred, because it can easily be confused with a Linux distribution, or another larger software distribution like Python itself.
Source Distributions vs Wheels
pip 可以从 Source Distributions (sdist) 或 Wheels 安装,但如果两者都在 PyPI 上,pip 会优先选择兼容的 Wheels。您可以通过使用 -no-binary 选项等方式覆盖 pip 的默认行为。
Wheels 是一种预编译的发行版格式,与源代码发行版 (sdist) 相比,安装速度更快,尤其是在项目包含编译扩展的情况下。
如果 pip 没有找到要安装的轮子,它会本地构建一个轮子并缓存起来,以备将来安装之用,而不是在将来重建源代码发行版。
pip can install from either Source Distributions (sdist) or Wheels, but if both are present on PyPI, pip will prefer a compatible wheel. You can override pip`s default behavior by e.g. using its –no-binary option.
Wheels are a pre-built distribution format that provides faster installation compared to Source Distributions (sdist), especially when a project contains compiled extensions.
If pip does not find a wheel to install, it will locally build a wheel and cache it for future installs, instead of rebuilding the source distribution in the future.
3, 测试和使用pip
以下命令将从 Python 包索引中安装模块及其依赖项的最新版本:
The following command will install the latest version of a module and its dependencies from the Python Package Index:
pip install package_name
or
pip3 install package_name
也可以使用python调用模块方式使用:
python -m pip install SomePackage
pip-install-test这个包是专门用来测试pip命令的。
用于测试 pip install 是否有效的存根包。此软件包的目的是提供一个可以轻松安装和卸载的简单模块。它提供了一种直接的机制,用于证明任意环境都能通过 pip 安装第三方模块。它并不打算使用 pip 或 setuptools 的全部功能集,而是提供一个最低限度的可行性 存在的证明。
A stub package for testing if pip install is working. The intent of this package is to provide a simple module that can be easily installed and uninstalled. This provides a straightforward mechanism for showing that an arbitrary environment is capable of installing third party modules via pip. It is not intended to exercise the full feature set of pip or setuptools, but to provide a minimal existence proof of viability.
Usage 
Install:
$ pip3 install pip-install-test
Collecting pip-install-test
  Downloading pip_install_test-0.5-py3-none-any.whl (1.7 kB)
Installing collected packages: pip-install-test
Successfully installed pip-install-test-0.5
Import:
>>> import pip_install_test
python -c 'import pip_install_test'
后面的字符串被当作命令来运行。
Uninstall:
$ pip3 uninstall pip-install-test
备注:
Installing from local archives,i nstall a particular source archive file:
python3 -m pip install ./downloads/SomeProject-1.0.4.tar.gz
要安装仅限当前用户而非系统所有用户使用的软件包,请使用 --user 标志:
To install packages that are isolated to the current user, rather than for all users of the system, use the --user flag:
python3 -m pip install --user SomeProject
Upgrade an already installed SomeProject to the latest from PyPI.
python3 -m pip install --upgrade SomeProject
To install the latest version of “SomeProject”:
python3 -m pip install "SomeProject"
要安装特定版本,应将软件包名称和版本括在双引号内:
To install a specific version, the package name and the version should be enclosed within double quotes:
python3 -m pip install "SomeProject==1.4"
To install greater than or equal to one version and less than another:
python3 -m pip install "SomeProject>=1,<2"
To install a version that’s compatible with a certain version:
python3 -m pip install "SomeProject~=1.4.2"
In this case, this means to install any version “==1.4.*” version that’s also “>=1.4.2”.
虽然仅靠 pip 就足以从预置的二进制压缩包中进行安装,但最新的 setuptools 和 wheel 项目副本对确保从源代码压缩包中进行安装也很有用:
While pip alone is sufficient to install from pre-built binary archives, up to date copies of the setuptools and wheel projects are useful to ensure you can also install from source archives:
python3 -m pip install --upgrade pip setuptools wheel
4,使用Ubuntu的包管理器来安装Python包
比如我在运行某个pyton脚本时,出现个错误:
错误:
import requests
ModuleNotFoundError: No module named 'requests'
解决方法:
For Debian/Ubuntu Python2: sudo apt-get install python-requests
For Debian/Ubuntu Python3: sudo apt-get install python3-requests
Windows
Use pip install requests (or pip3 install requests for python3) if you have pip installed and Pip.exe added to the Path Environment Variable. If pip is installed but not in your path you can use python -m pip install requests (or python3 -m pip install requests for python3)
按照提示,我运行了下面命令就OK了:
$ sudo apt-get install python3-requests
所以有一些包,使用Ubuntu的apt-get命令也能够安装。 apt-get用于从Canonical托管的Ubuntu软件库下载和安装软件包。
从apt-get和pip安装python包的一些区别如下:
- Canonical只提供选定的python模块的包。而PyPI承载了更多的python模块。所以,有很多python模块是无法用apt-get安装的。
- Canonical只托管任何软件包的单一版本(通常是最新的或最近发布的版本)。所以,用apt-get我们无法决定我们想要的python-package的版本。在这种情况下,pip可以帮助我们。我们可以安装以前在PyPI上上传过的任何版本的包。这在依赖关系发生冲突的情况下是非常有帮助的。
- apt-get 会将 python 模块安装到全系统位置。  而pip可以按照环境要求安装多个版本,分别适配不同的环境。
- 软件包的名字也会有差异。Canonical通常将Python 2软件包命名为python-<package_name>,Python 3软件包命名为python3-<package_name>。而对于 pip 来说,我们一般只需要对 Python 2 和 Python3 包都使用 <package_name>。
apt-get和pip都是成熟的软件包管理器,在安装时自动安装任何其他软件包的依赖。但是,如果你需要安装一个特定版本的python-package,或者安装一个只在PyPI上托管的软件包,或者有特殊安装需求,;只有pip会帮助你解决这个问题。否则,如果你不介意在全系统范围内安装软件包,那么使用apt-get或pip其实并不重要。
另外,apt-get本身是用来安装各种编程语言的包的。
参考:
pip-install-test · PyPI
Installing Packages - Python Packaging User Guide
software installation - apt-get install vs pip install - Ask Ubuntu
Installing Python Modules — Python 3.12.6 documentation

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2156645.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

intellij idea 控制台运行java出现中文乱码的解决方法

原因&#xff1a; 字符编码不一致&#xff1a; 当你在intellij idea使用了UTF-8编码&#xff0c;而在控制台使用了其他编码&#xff08;比如gbk&#xff09;&#xff0c;就可能导致乱码。 文件读写编码问题&#xff1a; 如果读取文件时使用的编码与文件实际编码不一致&#xf…

Chainlit集成LlamaIndex实现知识库高级检索(自动合并检索)

检索原理 自动合并检索 自动合并检索原理&#xff0c;和我的上一篇文章的检索方案&#xff1a; 将文本分割成512大小&#xff08;一般对应段落大小&#xff09;和128&#xff08;一般对句子大小不是严格的句子长度&#xff09;大小两种分别存储到索引库&#xff0c;再用llama_…

《深度学习》—— 卷积神经网络(CNN)的简单介绍和工作原理

文章目录 一、卷积神经网络的简单介绍二、工作原理(还未写完)1.输入层2.卷积层3.池化层4.全连接层5.输出层 一、卷积神经网络的简单介绍 基本概念 定义&#xff1a;卷积神经网络是一种深度学习模型&#xff0c;通常用于图像、视频、语音等信号数据的分类和识别任务。其核心思想…

如何在Markdown写文章上传到wordpress保证图片不丢失

如何在Markdown写文章上传到wordpress保证图片不丢失 写文日期,2023-11-16 引文 众所周知markdown是一款nb的笔记软件&#xff0c;本篇文章讲解如何在markdown编写文件后上传至wordpress论坛。并且保证图片不丢失&#xff08;将图片上传至云端而非本地方法&#xff09; 一&…

通信工程学习:什么是NFVI网络功能虚拟化基础设施层

NFVI&#xff1a;网络功能虚拟化基础设施层 NFVI&#xff08;Network Functions Virtualization Infrastructure&#xff09;即网络功能虚拟化基础设施层&#xff0c;是NFV&#xff08;Network Functions Virtualization&#xff0c;网络功能虚拟化&#xff09;架构中的一个重要…

精准农业中遥感技术应用(五)- 一站式遥感数据服务平台AIEarth

橙蜂智能公司致力于提供先进的人工智能和物联网解决方案&#xff0c;帮助企业优化运营并实现技术潜能。公司主要服务包括AI数字人、AI翻译、领域知识库、大模型服务等。其核心价值观为创新、客户至上、质量、合作和可持续发展。 橙蜂智农的智慧农业产品涵盖了多方面的功能&…

【LeetCode:116. 填充每个节点的下一个右侧节点指针 + BFS(层次遍历)】

&#x1f680; 算法题 &#x1f680; &#x1f332; 算法刷题专栏 | 面试必备算法 | 面试高频算法 &#x1f340; &#x1f332; 越难的东西,越要努力坚持&#xff0c;因为它具有很高的价值&#xff0c;算法就是这样✨ &#x1f332; 作者简介&#xff1a;硕风和炜&#xff0c;…

redis主从复制的理论和实战详细教程

0 前言 就是主从复制&#xff0c;master以写为主&#xff0c;slave以读为主&#xff0c;当master数据变化的时候&#xff0c;自动将新的数据异步同步到其他的slave数据库。也就是redis主从复制异步同步数据的&#xff0c;所以在主从架构中使用分布式锁时&#xff0c;可能会出现…

4--SpringBoot项目中分类管理

目录 新增分类 分类分页查询 启用禁用分类 根据类型查询 修改分类 本文介绍SpringBoot项目中的分类管理&#xff0c;操作类似员工管理模块&#xff0c;具体详解可见以下博客&#xff0c;此处给出各部分代码 2--SpringBoot项目中员工管理 详解&#xff08;一&#xff09;-C…

基于51单片机的手环设计仿真

目录 一、主要功能 二、硬件资源 三、程序编程 四、实现现象 一、主要功能 基于STC89C52单片机&#xff0c;DHT11温湿度采集温湿度&#xff0c;滑动变阻器连接ADC0832数模转换器模拟水位传感器检测水位&#xff0c;通过LCD1602显示信息&#xff0c;然后在程序里设置好是否…

vue3项目中引入词云图

在vue3中的项目引入词云图 前言&#xff1a;先看效果图步骤如下 前言&#xff1a; 公司产品要求项目中使用词云图&#xff0c;我算是第一次用&#xff0c;于是在网上查找资料&#xff0c;最后做出来了。 先看效果图 步骤如下 npm i echarts-wordcloud -S <template> …

恶意AI大模型的兴起将改变网络安全

LLM 的恶意版本&#xff08;如 ChatGPT 的黑暗变体&#xff09;的兴起正在通过使用更复杂和自动化的攻击来升级网络战。 这些模型可以生成令人信服的网络钓鱼电子邮件、传播虚假信息并制作有针对性的社会工程消息。 所有这些非法功能都对在线安全构成了重大威胁&#xff0c;并加…

2024年最新前端工程师 TypeScript 基础知识点详细教程(更新中)

1. TypeScript 概述 TypeScript 是由微软开发的、基于 JavaScript 的一种强类型编程语言。它是在 JavaScript 的基础上添加了静态类型检查、面向对象编程等功能的超集&#xff0c;最终会被编译为纯 JavaScript 代码。由于其扩展了 JavaScript 的功能&#xff0c;TypeScript 特…

[Redis][数据类型]详细讲解

1.Redis 特殊数据结构 1.Streams 应用场景&#xff1a;主要用为队列(阻塞队列) 2.Geospatial 应用场景&#xff1a;用来存储坐标(经纬度) 3.HyperLogLog 应用场景&#xff1a;估算集合中的元素个数注意&#xff1a; HyperLogLog不存储元素的内容&#xff0c;但是能够记录“…

【机器学习】ROC曲线

【机器学习】ROC曲线 1、ROC曲线简介2、ROC曲线和AUC值2.1 ROC曲线2.2 AUC值 3、实验内容3.1 准备数据集3.2 特征提取3.3 数据集划分3.4 模型训练与预测3.5 计算和绘制ROC曲线3.6 绘制混淆矩阵3.7 三分类混淆矩阵 4 源代码4.1 实现ROC二分类4.2 三分类混淆例子 1、ROC曲线简介 …

cnn机器学习时python版本不兼容报错

在使用python执行CNN算法时&#xff0c;发生如下报错&#xff1a; A module that was compiled using NumPy 1.x cannot be run in NumPy 2.1.1 as it may crash. To support both 1.x and 2.x versions of NumPy, modules must be compiled with NumPy 2.0. Some module may …

网络高级day03(Http)

目录 【1】HTTP简介 【2】 HTTP特点 【3】 HTTP协议格式 1》客户端请求消息格式 1> 请求行 2> 请求头 3> 空行 4> 请求数据 2》服务器响应消息格式 【1】HTTP简介 HTTP协议是Hyper Text Transfer Protocol &#xff08;超文本传输协议&#xff09;的缩写&a…

低代码平台:数据筛选功能的全新变革

随着软件开发需求的不断增长&#xff0c;传统的开发方法因其复杂性和耗时性而逐渐无法满足市场对快速交付和迭代的需求。低代码开发平台作为一种新型的软件开发工具&#xff0c;以其高效、易用的特点受到了广泛的关注和应用。 在软件开发领域&#xff0c;数据筛选是一项基础且…

frpc内网穿透

官网地址&#xff1a;frp官网 本次用到的Liunx包&#xff1a; https://github.com/fatedier/frp/releases/download/v0.60.0/frp_0.60.0_linux_amd64.tar.gz下载&#xff1a; wget https://github.com/fatedier/frp/releases/download/v0.60.0/frp_0.60.0_linux_amd64.tar.g…

经典大语言模型解读(3):参数量更大、泛化性能更强的生成式模型GPT-2

概述 在GPT-1的基础上&#xff0c;OpenAI提出了包含15亿参数&#xff08;GPT-1参数量的10倍以上&#xff09;的GPT-2模型。该模型在一个更大规模的文本数据集WebText上进行预训练。与GPT-1依赖特定任务上的有监督微调来提升性能不同&#xff0c;GPT-2具备更强的零样本&#xf…