mirror of
https://github.com/LCTT/TranslateProject.git
synced 2025-01-25 23:11:02 +08:00
translated
This commit is contained in:
parent
5f78e01ff3
commit
039f085be3
@ -1,159 +0,0 @@
|
|||||||
[#]: collector: (lujun9972)
|
|
||||||
[#]: translator: (geekpi)
|
|
||||||
[#]: reviewer: ( )
|
|
||||||
[#]: publisher: ( )
|
|
||||||
[#]: url: ( )
|
|
||||||
[#]: subject: (How to install Python on Linux)
|
|
||||||
[#]: via: (https://opensource.com/article/20/4/install-python-linux)
|
|
||||||
[#]: author: (Vijay Singh Khatri https://opensource.com/users/vijaytechnicalauthor)
|
|
||||||
|
|
||||||
How to install Python on Linux
|
|
||||||
======
|
|
||||||
Step-by-step instructions for installing the latest Python instead of
|
|
||||||
(or alongside) an older version on Linux.
|
|
||||||
![OpenStack source code \(Python\) in VIM][1]
|
|
||||||
|
|
||||||
[Python][2] is now the [most popular][3], most used programming language. Python's simple syntax and low learning curve make it the ultimate choice for beginners as well as professional developers. Python is also a very versatile programming language. It's used nearly everywhere—from web development to artificial intelligence—really anywhere other than mobile development.
|
|
||||||
|
|
||||||
If you're using Python, there's a good chance you're a developer (or want to become one), and Linux is a great platform for creating software. But when you're working with Python every day, you sometimes want to stay up to date with the very latest version. You may not want to replace the default install of Python on your system just to test drive the latest one, so this article explains how to install the latest version of Python 3 on Linux without replacing the version provided by your distribution.
|
|
||||||
|
|
||||||
Use the **python --version terminal** command to check whether Python is already installed and, if so, which version you have. If Python is not installed on your Linux system, or you want to install an updated version, follow the steps below.
|
|
||||||
|
|
||||||
### Step-by-step installation instructions
|
|
||||||
|
|
||||||
#### **Step 1:** First, install development packages required to build Python.
|
|
||||||
|
|
||||||
#### On Debian:
|
|
||||||
|
|
||||||
|
|
||||||
```
|
|
||||||
$ sudo apt update
|
|
||||||
$ sudo apt install build-essential zlib1g-dev \
|
|
||||||
libncurses5-dev libgdbm-dev libnss3-dev \
|
|
||||||
libssl-dev libreadline-dev libffi-dev curl
|
|
||||||
```
|
|
||||||
|
|
||||||
#### On Fedora:
|
|
||||||
|
|
||||||
|
|
||||||
```
|
|
||||||
`$ sudo dnf groupinstall development`
|
|
||||||
```
|
|
||||||
|
|
||||||
#### Step 2: Download the stable latest release of Python 3
|
|
||||||
|
|
||||||
Visit the [official Python website][4] and download the latest version of Python 3. After the download is complete, you hav a **.tar.xz** archive file (a "tarball") containing the source code of Python.
|
|
||||||
|
|
||||||
#### Step 3: Extract the tarball
|
|
||||||
|
|
||||||
Once the download is complete, extract the tarball by either using the extractor application of your choice or the [Linux **tar** command][5], for example:
|
|
||||||
|
|
||||||
|
|
||||||
```
|
|
||||||
`$ tar -xf Python-3.?.?.tar.xz`
|
|
||||||
```
|
|
||||||
|
|
||||||
#### Step 4: Configure the script
|
|
||||||
|
|
||||||
Once the Python tarball has been extracted, navigate to the configure script and execute it in your Linux terminal with:
|
|
||||||
|
|
||||||
|
|
||||||
```
|
|
||||||
$ cd Python-3.*
|
|
||||||
./configure
|
|
||||||
```
|
|
||||||
|
|
||||||
The configuration may take some time. Wait until it is successfully finishes before proceeding.
|
|
||||||
|
|
||||||
#### Step 5: Start the build process
|
|
||||||
|
|
||||||
If you already have a version of Python installed on your system and you want to install the new version alongside it, use this command:
|
|
||||||
|
|
||||||
|
|
||||||
```
|
|
||||||
`$ sudo make altinstall`
|
|
||||||
```
|
|
||||||
|
|
||||||
The build process may take some time.
|
|
||||||
|
|
||||||
If you want to replace your current version of Python with this new version, you should uninstall your current Python package using your package manager (such as **apt** or **dnf**) and then install:
|
|
||||||
|
|
||||||
|
|
||||||
```
|
|
||||||
`$ sudo make install`
|
|
||||||
```
|
|
||||||
|
|
||||||
However, it's generally preferable to install software as a package (such as a **.deb** or **.rpm** file) so your system can track and update it for you. Because this article assumes the latest Python isn't yet packaged yet, though, you probably don't have that option. In that case, you can either install Python with **altinstall** as suggested, or rebuild an existing Python package using the latest source code. That's an advanced topic and specific to your distribution, so it's out of scope for this article.
|
|
||||||
|
|
||||||
#### Step 6: Verify the installation
|
|
||||||
|
|
||||||
If you haven't encountered any errors, the latest Python is now installed on your Linux system. To verify it, write one of these commands in your terminal:
|
|
||||||
|
|
||||||
|
|
||||||
```
|
|
||||||
`python3 --version`
|
|
||||||
```
|
|
||||||
|
|
||||||
or
|
|
||||||
|
|
||||||
|
|
||||||
```
|
|
||||||
`python --version`
|
|
||||||
```
|
|
||||||
|
|
||||||
If the output says **Python 3.x**, Python 3 has been successfully installed.
|
|
||||||
|
|
||||||
### Create a virtual environment (optional)
|
|
||||||
|
|
||||||
Python provides a package known as **venv** (virtual environment), which helps you isolate a program directory or package from other ones.
|
|
||||||
|
|
||||||
To create a virtual environment, enter the following in the Python terminal (in this example, assume the version of Python you've installed is in the **3.8** series):
|
|
||||||
|
|
||||||
|
|
||||||
```
|
|
||||||
`python3.8 -m venv example`
|
|
||||||
```
|
|
||||||
|
|
||||||
This command creates a new directory (which I've named **example**), with some subdirectories.
|
|
||||||
|
|
||||||
To activate the virtual environment, enter:
|
|
||||||
|
|
||||||
|
|
||||||
```
|
|
||||||
$ source example/bin/activate
|
|
||||||
(example) $
|
|
||||||
```
|
|
||||||
|
|
||||||
Notice that your terminal prompt (**$**) is now preceeded by an environment name.
|
|
||||||
|
|
||||||
To deactivate the virtual environment, use the **deactivate** command:
|
|
||||||
|
|
||||||
|
|
||||||
```
|
|
||||||
`(example) $ deactivate`
|
|
||||||
```
|
|
||||||
|
|
||||||
### Conclusion
|
|
||||||
|
|
||||||
Python is a fun language that's developed and improved frequently. Getting familiar with new features is easy, once you understand how to install the latest release without interfering with the stable version provided from your distribution.
|
|
||||||
|
|
||||||
If you have any feedback or questions, please leave them in the comments.
|
|
||||||
|
|
||||||
--------------------------------------------------------------------------------
|
|
||||||
|
|
||||||
via: https://opensource.com/article/20/4/install-python-linux
|
|
||||||
|
|
||||||
作者:[Vijay Singh Khatri][a]
|
|
||||||
选题:[lujun9972][b]
|
|
||||||
译者:[译者ID](https://github.com/译者ID)
|
|
||||||
校对:[校对者ID](https://github.com/校对者ID)
|
|
||||||
|
|
||||||
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
|
||||||
|
|
||||||
[a]: https://opensource.com/users/vijaytechnicalauthor
|
|
||||||
[b]: https://github.com/lujun9972
|
|
||||||
[1]: https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/openstack_python_vim_1.jpg?itok=lHQK5zpm (OpenStack source code (Python) in VIM)
|
|
||||||
[2]: https://www.python.org/
|
|
||||||
[3]: http://pypl.github.io/PYPL.html
|
|
||||||
[4]: http://python.org
|
|
||||||
[5]: https://opensource.com/article/17/7/how-unzip-targz-file
|
|
157
translated/tech/20200413 How to install Python on Linux.md
Normal file
157
translated/tech/20200413 How to install Python on Linux.md
Normal file
@ -0,0 +1,157 @@
|
|||||||
|
[#]: collector: (lujun9972)
|
||||||
|
[#]: translator: (geekpi)
|
||||||
|
[#]: reviewer: ( )
|
||||||
|
[#]: publisher: ( )
|
||||||
|
[#]: url: ( )
|
||||||
|
[#]: subject: (How to install Python on Linux)
|
||||||
|
[#]: via: (https://opensource.com/article/20/4/install-python-linux)
|
||||||
|
[#]: author: (Vijay Singh Khatri https://opensource.com/users/vijaytechnicalauthor)
|
||||||
|
|
||||||
|
如何在 Linux 上安装 Python
|
||||||
|
======
|
||||||
|
在 Linux 上安装最新 Python (或并列多个)而不是老版本的分步说明。
|
||||||
|
![OpenStack source code \(Python\) in VIM][1]
|
||||||
|
|
||||||
|
[Python][2] 现在是[最受欢迎][3],最常用的编程语言。Python 的简单语法和较低的学习曲线使其成为初学者和专业开发人员的最终选择。Python 还是一种非常通用的编程语言。从 Web 开发到人工智能,它几乎在除了移动开发的所有地方都有使用。
|
||||||
|
|
||||||
|
如果你使用的是 Python,那么你很有可能是一名开发人员(或想成为一名开发人员),而 Linux 是创建软件的绝佳平台。但是,当你每天使用 Python 时,有时你希望使用最新版本。你可能不想仅仅为了测试最新版本的系统而替换了默认的 Python 安装,因此本文说明了如何在 Linux 上安装最新版本的 Python 3,而不替换发行版提供的版本。
|
||||||
|
|
||||||
|
使用 **python --version terminal** 命令检查是否已安装 Python,如果已安装,那么检查是哪个版本。如果你的 Linux 系统上未安装 Python,或者你想安装更新的版本,请按照以下步骤操作。
|
||||||
|
|
||||||
|
### 分步安装说明
|
||||||
|
|
||||||
|
#### **步骤 1:**首先,安装构建 Python 所需的开发包。
|
||||||
|
|
||||||
|
#### 在 Debian 上:
|
||||||
|
|
||||||
|
|
||||||
|
```
|
||||||
|
$ sudo apt update
|
||||||
|
$ sudo apt install build-essential zlib1g-dev \
|
||||||
|
libncurses5-dev libgdbm-dev libnss3-dev \
|
||||||
|
libssl-dev libreadline-dev libffi-dev curl
|
||||||
|
```
|
||||||
|
|
||||||
|
#### 在 Fedora 上:
|
||||||
|
|
||||||
|
|
||||||
|
```
|
||||||
|
`$ sudo dnf groupinstall development`
|
||||||
|
```
|
||||||
|
|
||||||
|
#### 步骤 2:下载稳定的最新版本的 Python 3
|
||||||
|
|
||||||
|
访问[官方 Python 网站][4]并下载最新版本的 Python3。下载完成后,你会有一个 **.tar.xz** 归档文件(“tarball”),其中包含 Python 的源代码。
|
||||||
|
|
||||||
|
#### 步骤 3:解压 tarball
|
||||||
|
|
||||||
|
下载完成后,使用解压程序或 [Linux 的 **tar** 命令][5]解压压缩包,例如:
|
||||||
|
|
||||||
|
```
|
||||||
|
`$ tar -xf Python-3.?.?.tar.xz`
|
||||||
|
```
|
||||||
|
|
||||||
|
#### 步骤 4:配置脚本
|
||||||
|
|
||||||
|
解压 Python 压缩包后,进入 configure 脚本所在目录并在 Linux 终端中使用以下命令执行该脚本:
|
||||||
|
|
||||||
|
|
||||||
|
```
|
||||||
|
$ cd Python-3.*
|
||||||
|
./configure
|
||||||
|
```
|
||||||
|
|
||||||
|
配置可能需要一些时间。等待直到成功完成,然后再继续。
|
||||||
|
|
||||||
|
#### 步骤 5:开始构建过程
|
||||||
|
|
||||||
|
如果你的系统上已经安装了某个版本的 Python,并且希望同时安装新版本的 Python,请使用以下命令:
|
||||||
|
|
||||||
|
|
||||||
|
```
|
||||||
|
`$ sudo make altinstall`
|
||||||
|
```
|
||||||
|
|
||||||
|
构建过程可能需要一些时间。
|
||||||
|
|
||||||
|
如果要使用此版本替换当前版本的 Python,那么应使用包管理器(例如 **apt** 或 **dnf**)卸载当前的 Python 包,然后安装:
|
||||||
|
|
||||||
|
|
||||||
|
```
|
||||||
|
`$ sudo make install`
|
||||||
|
```
|
||||||
|
|
||||||
|
但是,通常最好将软件作为包安装(例如 **.deb** 或 **.rpm** 文件),以便系统可以为你跟踪和更新它。因为本文假设尚未打包最新的 Python,所以你可能没有这个选择。在这种情况下,你可以按照建议使用 **altinstall** 来安装 Python,或者使用最新的源代码重建现有的 Python 包。这是一个高级主题,并且特定于你的发行版,因此不在本文讨论范围之内。
|
||||||
|
|
||||||
|
#### 步骤 6:验证安装
|
||||||
|
|
||||||
|
如果你没有遇到任何错误,那么现在你的 Linux 系统上已安装了最新的 Python。要进行验证,请在终端中输入以下命令之一:
|
||||||
|
|
||||||
|
|
||||||
|
```
|
||||||
|
`python3 --version`
|
||||||
|
```
|
||||||
|
|
||||||
|
或者
|
||||||
|
|
||||||
|
|
||||||
|
```
|
||||||
|
`python --version`
|
||||||
|
```
|
||||||
|
|
||||||
|
如果输出显示 **Python 3.x**,那么说明 Python 3 已成功安装。
|
||||||
|
|
||||||
|
### 创建虚拟环境(可选)
|
||||||
|
|
||||||
|
Python 提供了名为 **venv**(虚拟环境)的软件包,可帮助你将程序目录或软件包与其他目录或软件包隔离。
|
||||||
|
|
||||||
|
要创建虚拟环境,请在 Python 终端中输入以下内容(在此示例中,假定你安装的 Python 版本为 **3.8** 系列):
|
||||||
|
|
||||||
|
|
||||||
|
```
|
||||||
|
`python3.8 -m venv example`
|
||||||
|
```
|
||||||
|
|
||||||
|
该命令创建一个带有一些子目录的新目录(我将其命名为 **example**)。
|
||||||
|
|
||||||
|
要激活虚拟环境,请输入:
|
||||||
|
|
||||||
|
|
||||||
|
```
|
||||||
|
$ source example/bin/activate
|
||||||
|
(example) $
|
||||||
|
```
|
||||||
|
|
||||||
|
请注意,你的终端提示符(**$**)现在以环境名称开头。
|
||||||
|
|
||||||
|
要停用虚拟环境,请使用 **deactivate** 命令:
|
||||||
|
|
||||||
|
|
||||||
|
```
|
||||||
|
`(example) $ deactivate`
|
||||||
|
```
|
||||||
|
|
||||||
|
### 总结
|
||||||
|
|
||||||
|
Python 是一种有趣的语言,经常被开发和改进。一旦了解了如何安装最新版本而又不干扰发行版提供的稳定版本,熟悉新功能将很容易。
|
||||||
|
|
||||||
|
如果你有任何反馈或问题,请在评论中提出。
|
||||||
|
|
||||||
|
--------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
via: https://opensource.com/article/20/4/install-python-linux
|
||||||
|
|
||||||
|
作者:[Vijay Singh Khatri][a]
|
||||||
|
选题:[lujun9972][b]
|
||||||
|
译者:[geekpi](https://github.com/geekpi)
|
||||||
|
校对:[校对者ID](https://github.com/校对者ID)
|
||||||
|
|
||||||
|
本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译,[Linux中国](https://linux.cn/) 荣誉推出
|
||||||
|
|
||||||
|
[a]: https://opensource.com/users/vijaytechnicalauthor
|
||||||
|
[b]: https://github.com/lujun9972
|
||||||
|
[1]: https://opensource.com/sites/default/files/styles/image-full-size/public/lead-images/openstack_python_vim_1.jpg?itok=lHQK5zpm (OpenStack source code (Python) in VIM)
|
||||||
|
[2]: https://www.python.org/
|
||||||
|
[3]: http://pypl.github.io/PYPL.html
|
||||||
|
[4]: http://python.org
|
||||||
|
[5]: https://opensource.com/article/17/7/how-unzip-targz-file
|
Loading…
Reference in New Issue
Block a user