如何在CentOS 8上安装Python 3和设置编程环境

介绍 (Introduction)

A versatile programming language, Python can be used for many different programming projects. Inspired by the British comedy group Monty Python, the development team behind Python wanted to make a language that was fun to use. An increasingly popular language with many different applications, Python is a great choice for beginners and experienced developers alike.

Python是一种通用的编程语言,可用于许多不同的编程项目。 受英国喜剧团体Monty Python的启发,Python背后的开发团队希望开发一种有趣的语言。 Python是一种越来越流行的语言,具有许多不同的应用程序,是初学者和经验丰富的开发人员的理想选择。

This tutorial will guide you through installing Python 3 on a CentOS 8 cloud server and setting up a programming environment via the command line.

本教程将指导您在CentOS 8云服务器上安装Python 3并通过命令行设置编程环境。

先决条件 (Prerequisites)

You will need a CentOS 8 server with a non-root superuser account.

您将需要具有非超级用户超级帐户的CentOS 8服务器。

To set this up, you can follow our Initial Server Setup Guide for CentOS 8.

要进行设置,您可以遵循我们的《 CentOS 8初始服务器设置指南》

第1步-准备系统 (Step 1 — Preparing the System)

Before we begin with the installation, let’s make sure to update the default system applications to ensure we have the latest versions available.

在开始安装之前,请确保更新默认的系统应用程序,以确保拥有可用的最新版本。

We will be using the open-source package manager tool DNF, which stands for Dandified YUM the next-generation version of the Yellowdog Updater, Modified (that is, yum). DNF is a package manager that is now the default package manager for Red Hat based Linux systems like CentOS. It will let you install, update, and remove software packages on your server.

我们将使用开源程序包管理器工具DNF ,它代表Dandified YUM ,即Yellowdog Updater Modified(即yum )的下一代版本。 DNF是一个程序包管理器,现在是基于Red Hat的Linux系统(如CentOS)的默认程序包管理器。 它使您可以在服务器上安装,更新和删除软件包。

Let’s first make sure that our package manager is up to date by running this command:

首先,通过运行以下命令确保我们的软件包管理器是最新的:

  • sudo dnf update -y

    须藤dnf更新-y

The -y flag is used to alert the system that we are aware that we are making changes, preventing the terminal from prompting us to confirm.

-y标志用于警告系统我们知道我们正在进行更改,从而防止终端提示我们进行确认。

Once everything is installed, our setup is in place and we can go on to install Python 3.

一切安装完成后,我们的设置就位了,我们可以继续安装Python 3了。

第2步-安装和设置Python 3 (Step 2 — Installing and Setting Up Python 3)

CentOS is derived from RHEL (Red Hat Enterprise Linux), which has stability as its primary focus. Because of this, tested and stable versions of applications are what is most commonly found on the system and in downloadable packages, so using the CentOS package manager you will find earlier versions of Python than the most recent release.

CentOS源自RHEL(红帽企业Linux),RHEL以稳定性为主要重点。 因此,经过测试且稳定的应用程序版本是系统和可下载软件包中最常见的版本,因此,使用CentOS软件包管理器,您将找到比最新发行版更早的Python版本。

  • sudo dnf install python3 -y

    须藤dnf安装python3 -y

When this process is complete, we can check to make sure that the installation was successful by checking for its version number with the python3 command:

完成此过程后,我们可以通过使用python3命令检查其版本号来检查安装是否成功:

  • python3 -V

    python3 -V

With a version of Python 3 successfully installed, we will receive the following output:

成功安装Python 3版本后,我们将收到以下输出:


   
   
Output
Python 3.6.8

Next, we’ll install the CentOS Development Tools, which are used to allow you to build and compile software from source code:

接下来,我们将安装CentOS开发工具,这些工具可用于允许您从源代码构建和编译软件:

  • sudo dnf -y groupinstall development

    sudo dnf -y groupinstall开发

With that installed, we’ll go over how to set up Python development projects in the next section.

安装该工具后,我们将在下一部分中介绍如何设置Python开发项目。

步骤3 —设置虚拟环境 (Step 3 — Setting Up a Virtual Environment)

With Python installed and our system set up, we can go on to create our programming environment with venv.

安装Python和设置系统后,我们可以继续使用venv创建我们的编程环境。

Virtual environments enable you to have an isolated space on your computer for Python projects, ensuring that each of your projects can have its own set of dependencies that won’t disrupt any of your other projects.

虚拟环境使您可以在计算机上拥有一个用于Python项目的隔离空间,从而确保每个项目都可以拥有自己的一组依赖关系,而这些依赖关系不会破坏任何其他项目。

Setting up a programming environment provides us with greater control over our Python projects, as well as over different packages and versions. This is especially important when working with third-party packages.

设置编程环境使我们可以更好地控制Python项目以及不同的软件包和版本。 在使用第三方软件包时,这一点尤其重要。

You can set up as many Python programming environments as you would like. Each environment is essentially a directory or folder on your server that has a few scripts to set it up as an environment.

您可以根据需要设置任意数量的Python编程环境。 每个环境本质上都是服务器上的目录或文件夹,具有一些脚本可将其设置为环境。

Choose which directory you would like to put your Python programming environments in, or create a new directory with mkdir, as in:

选择您要放置Python编程环境的目录,或使用mkdir创建一个新目录,如下所示:

  • mkdir environments

    mkdir 环境

  • cd environments

    cd 环境

Once you are in the directory where you would like the environments to live, you can create an environment by running the following command. You should use an environment name that makes sense for you, here we are calling it my_env.

一旦进入您希望环境存在的目录,就可以通过运行以下命令来创建环境。 您应该使用一个对您有意义的环境名称,这里我们将其my_env

  • python3 -m venv my_env

    python3 -m venv my_env

In this case the environment is my_env, and this new directory contains a few items that we can display if we use the ls command in that directory:

在这种情况下,环境是my_env ,这个新目录包含一些如果我们在该目录中使用ls命令可以显示的项目:


   
   
Output
bin include lib lib64 pyvenv.cfg

Together, these files work to isolate your Python work from the broader context of your local machine, so that system files and project files don’t mix. This is good practice for version control and to ensure that each of your projects has access to the particular packages that it needs.

这些文件结合在一起,可以将您的Python工作与本地计算机的更广泛的上下文隔离开,从而避免系统文件和项目文件混在一起。 这是进行版本控制并确保您的每个项目都可以访问所需的特定程序包的良好做法。

To use this environment, you need to activate it, which you can do by typing the following command that calls the activate script in the bin directory:

要使用此环境,您需要激活它,您可以通过在bin目录中键入以下调用激活脚本的命令来激活它:

  • source my_env/bin/activate

    源my_env / bin / activate

Your prompt will now be prefixed with the name of your environment, in this case it is called my_env:

现在,您的提示将以您的环境名称为前缀,在这种情况下,它称为my_env :

This prefix lets us know that the environment my_env is currently active, meaning that when we create programs here they will use only this particular environment’s settings and packages.

该前缀使我们知道环境my_env当前处于活动状态,这意味着在此处创建程序时,它们将仅使用此特定环境的设置和程序包。

The Python package manager pip is already installed. A tool for use with Python, we will use pip to install and manage programming packages we may want to use in our development projects. You can install Python packages by typing:

Python软件包管理器pip已安装。 这是与Python一起使用的工具,我们将使用pip来安装和管理我们可能要在开发项目中使用的编程包。 您可以通过输入以下命令安装Python软件包:

  • sudo pip install package_name

    sudo pip安装package_name

Here, package_name can refer to any Python package or library, such as Django for web development or NumPy for scientific computing. So if you would like to install NumPy, you can do so with the command pip install numpy.

在这里, package_name可以引用任何Python包或库,例如用于Web开发的Django或用于科学计算的NumPy。 因此,如果您想安装NumPy,则可以使用命令pip install numpy

Note: Within the virtual environment, you can use the command python instead of python3, and pip instead of pip3. If you use Python 3 or pip3 on your machine outside of an environment, you will need to use the python3 and pip3 commands exclusively.

注意:在虚拟环境中,可以使用命令python代替python3 ,并使用pip代替pip3 。 如果您在环境之外的计算机上使用Python 3或pip3,则将需要专门使用python3pip3命令。

After following these steps, your virtual environment is ready to use.

完成这些步骤后,即可使用虚拟环境。

第4步-创建一个“你好,世界!” 程序 (Step 4 — Creating a “Hello, World!” Program)

Now that we have our virtual environment set up, let’s create the traditional “Hello, World!” program to test our installation. This will make sure that our environment is working and gives us the opportunity to become more familiar with Python if we aren’t already.

现在我们已经建立了虚拟环境,让我们创建传统的“ Hello,World!”。 程序来测试我们的安装。 这将确保我们的环境能够正常工作,并且使我们有机会熟悉Python(如果尚未熟悉)。

To do this, we’ll open up a command-line text editor such as vi and create a new file:

为此,我们将打开一个命令行文本编辑器(如vi)并创建一个新文件:

  • vi hello.py

    vi hello.py

Once the text file opens up in our terminal window, we will have to type i to enter insert mode, and then we can write our first program:

一旦文本文件在终端窗口中打开,我们将必须输入i进入插入模式,然后我们可以编写第一个程序:

print("Hello, World!")

Now press ESC to leave insert mode. Next, type :x then ENTER to save and exit the file.

现在按ESC退出插入模式。 接下来,键入:x然后按ENTER以保存并退出文件。

We are now ready to run our program:

现在,我们可以运行我们的程序了:

  • python hello.py

    python hello.py

The hello.py program that you just created should cause the terminal to produce the following output:

您刚刚创建的hello.py程序应使终端产生以下输出:


   
   
Output
Hello, World!

To leave the environment, type the command deactivate and you’ll return to your original directory.

要退出环境,请键入命令deactivate ,您将返回到原始目录。

结论 (Conclusion)

Congratulations! At this point you have a Python 3 programming environment set up on your CentOS 8 server and can begin a coding project!

恭喜你! 至此,您已经在CentOS 8服务器上设置了Python 3编程环境,并且可以开始编码项目!

With your machine ready for software development, you can continue to learn more about coding in Python by following along with our How To Code in Python series, or downloading the free HowTo Code in Python eBook.

在准备好进行软件开发的机器后,您可以继续阅读我们的Python编码方法系列 ,或下载免费的Python eBook中的HowTo代码,以进一步了解Python编码

To explore machine learning projects in particular, refer to our Python Machine Learning Projects eBook.

要特别探索机器学习项目,请参阅我们的Python机器学习项目电子书

翻译自: https://www.digitalocean.com/community/tutorials/how-to-install-python-3-and-set-up-a-programming-environment-on-centos-8

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值