如何在Ubuntu 20.04上安装Python 3和设置编程环境[快速入门]

介绍 (Introduction)

Python is a flexible and versatile programming language, with strengths in scripting, automation, data analysis, machine learning, and back-end development.

Python是一种灵活而通用的编程语言,在脚本,自动化,数据分析,机器学习和后端开发方面具有优势。

This tutorial will walk you through installing Python and setting up a programming environment on an Ubuntu 20.04 server. For a more detailed version of this tutorial, with more thorough explanations of each step, please refer to How To Install Python 3 and Set Up a Programming Environment on an Ubuntu 20.04 Server.

本教程将引导您完成在Ubuntu 20.04服务器上安装Python和设置编程环境的过程。 有关本教程的更详细版本,以及每个步骤的更详尽说明,请参阅如何在Ubuntu 20.04 Server上安装Python 3和设置编程环境 。

步骤1 —更新和升级 (Step 1 — Update and Upgrade)

Logged into your Ubuntu 20.04 server as a sudo non-root user, first update and upgrade your system to ensure that your shipped version of Python 3 is up-to-date.

以sudo非root用户身份登录到Ubuntu 20.04服务器,首先更新和升级系统,以确保所提供的Python 3版本是最新的。

  • sudo apt update

    sudo apt更新
  • sudo apt -y upgrade

    sudo apt -y升级

Confirm installation if prompted to do so.

如果出现提示,请确认安装。

第2步-检查Python版本 (Step 2 — Check Version of Python)

Check which version of Python 3 is installed by typing:

通过键入以下命令检查安装了哪个版本的Python 3:

  • python3 -V

    python3 -V

You’ll receive output similar to the following, depending on when you have updated your system.

您将收到类似于以下内容的输出,具体取决于您更新系统的时间。


   
     
     
     
     
Output
Python 3.8.2

步骤3 —安装点子 (Step 3 — Install pip)

To manage software packages for Python, install pip, a tool that will help you manage libraries or modules to use in your projects.

要管理适用于Python的软件包,请安装pip ,该工具将帮助您管理要在项目中使用的库或模块。

  • sudo apt install -y python3-pip

    须藤apt install -y python3-pip

Python packages can be installed by typing:

可以通过输入以下命令安装Python软件包:

  • pip3 install package_name

    pip3安装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 pip3 install numpy.

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

步骤4 —安装其他工具 (Step 4 — Install Additional Tools)

There are a few more packages and development tools to install to ensure that we have a robust set-up for our programming environment:

还有更多的软件包和开发工具可安装,以确保我们对编程环境具有健全的设置:

  • sudo apt install build-essential libssl-dev libffi-dev python3-dev

    须藤apt install build-essential libssl-dev libffi-dev python3-dev

步骤5 —安装venv (Step 5 — Install venv)

Virtual environments enable you to have an isolated space on your server for Python projects. We’ll use venv, part of the standard Python 3 library, which we can install by typing:

虚拟环境使您可以在服务器上为Python项目提供隔离的空间。 我们将使用venv ,它是标准Python 3库的一部分,可以通过键入以下内容进行安装:

  • sudo apt install -y python3-venv

    须藤apt install -y python3-venv

第6步-创建虚拟环境 (Step 6 — Create a Virtual Environment)

You can create a new environment with the pyvenv command. Here, we’ll call our new environment my_env, but you should call yours something meaningful to your project.

您可以使用pyvenv命令创建一个新环境。 在这里,我们将新环境my_env ,但您应该为自己的项目命名一些有意义的东西。

  • python3 -m venv my_env

    python3 -m venv my_env

步骤7 —激活虚拟环境 (Step 7 — Activate Virtual Environment)

Activate the environment using the command below, where my_env is the name of your programming environment.

使用下面的命令激活环境,其中my_env是您的编程环境的名称。

  • source my_env/bin/activate

    源my_env / bin / activate

Your command prompt will now be prefixed with the name of your environment:

现在,您的命令提示符将以您的环境名称为前缀:

步骤8 —测试虚拟环境 (Step 8 — Test Virtual Environment)

Open the Python interpreter:

打开Python解释器:

  • python

    Python

Note that within the Python 3 virtual environment, you can use the command python instead of python3, and pip instead of pip3.

请注意,在Python 3虚拟环境中,您可以使用命令python代替python3 ,并使用pip代替pip3

You’ll know you’re in the interpreter when you receive the following output:

当您收到以下输出时,您将知道您在解释器中:

Python 3.8.2 (default, Mar 13 2020, 10:14:16) 
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>

Now, use the print() function to create the traditional Hello, World program:

现在,使用print()函数创建传统的Hello,World程序:

  • print("Hello, World!")

    打印(“ Hello,World!”)

   
     
     
     
     
Output
Hello, World!

步骤9 —停用虚拟环境 (Step 9 — Deactivate Virtual Environment)

Quit the Python interpreter:

退出Python解释器:

  • quit()

    退出()

Then exit the virtual environment:

然后退出虚拟环境:

  • deactivate

    停用

进一步阅读 (Further Reading)

From here, there is a lot you can learn about Python, here are some links related to this guide:

从这里,您可以学到很多关于Python的知识,这里有一些与本指南相关的链接:

  • How To Install Python 3 and Set Up a Programming Environment on an Ubuntu 20.04 Server

    如何在Ubuntu 20.04服务器上安装Python 3和设置编程环境

  • Free How To Code in Python 3 eBook

    免费使用Python 3电子书进行编码

  • Python Tutorials

    Python教程

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

你可能感兴趣的:(编程语言,python,java,linux,人工智能)