自学内容网 自学内容网

Linux - 在Ubuntu中安装Python和PIP

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

原文地址:https://blog.csdn.net/guoqx/article/details/142431137

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!