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

介绍 (Introduction)

Python is a flexible and versatile programming language that can be leveraged for many use cases, with strengths in scripting, automation, data analysis, machine learning, and back-end development. First published in 1991 with a name inspired by the British comedy group Monty Python, the development team wanted to make Python a language that was fun to use. Quick to set up, and written in a relatively straightforward style with immediate feedback on errors, Python is a great choice for beginners and experienced developers alike. Python 3 is the most current version of the language and is considered to be the future of Python.

Python是一种灵活且通用的编程语言,可在许多用例中利用,在脚本,自动化,数据分析,机器学习和后端开发方面具有优势。 开发团队于1991年首次发布,其名称受到英国喜剧团体Monty Python的启发,开发团队希望使Python成为一种有趣的语言。 Python设置Swift,并且以相对简单的风格编写,可以立即对错误进行反馈,对于初学者和经验丰富的开发人员而言,Python是一个不错的选择。 Python 3是该语言的最新版本 ,被认为是Python的未来。

This tutorial will get your Ubuntu 18.04 server set up with a Python 3 programming environment. Programming on a server has many advantages and supports collaboration across development projects. The general principles of this tutorial will apply to any distribution of Debian Linux.

本教程将为您的Ubuntu 18.04服务器设置Python 3编程环境。 在服务器上编程具有许多优点,并支持跨开发项目的协作。 本教程的一般原则将适用于Debian Linux的任何发行版。

先决条件 (Prerequisites)

In order to complete this tutorial, you should have a non-root user with sudo privileges on an Ubuntu 18.04 server. To learn how to achieve this setup, follow our manual initial server setup guide or run our automated script.

为了完成本教程,您应该在Ubuntu 18.04服务器上拥有一个具有sudo特权的非root用户。 要了解如何实现此设置,请遵循我们的手动初始服务器设置指南或运行我们的自动化脚本 。

If you’re not already familiar with a terminal environment, you may find the article “An Introduction to the Linux Terminal” useful for becoming better oriented with the terminal.

如果您还不熟悉终端环境,则可能会发现文章“ Linux终端简介 ”对于更好地适应终端环境很有用。

With your server and user set up, you are ready to begin.

设置好服务器和用户后,就可以开始了。

第1步-设置Python 3 (Step 1 — Setting Up Python 3)

Ubuntu 18.04 and other versions of Debian Linux ship with both Python 3 and Python 2 pre-installed. To make sure that our versions are up-to-date, let’s update and upgrade the system with the apt command to work with Ubuntu’s Advanced Packaging Tool:

Ubuntu 18.04和其他版本的Debian Linux都预装了Python 3和Python 2。 为了确保我们的版本是最新的更新,让我们更新,并与升级系统apt命令与Ubuntu的一个先进适用的工作ackaging P OOL 牛逼

  • sudo apt update

    sudo apt更新
  • sudo apt -y upgrade

    sudo apt -y升级

The -y flag will confirm that we are agreeing for all items to be installed, but depending on your version of Linux, you may need to confirm additional prompts as your system updates and upgrades.

-y标志将确认我们同意安装所有项目,但是根据您的Linux版本,您可能需要在系统更新和升级时确认其他提示。

Once the process is complete, we can check the version of Python 3 that is installed in the system by typing:

该过程完成后,我们可以通过输入以下命令检查系统中安装的Python 3版本:

  • python3 -V

    python3 -V

You’ll receive output in the terminal window that will let you know the version number. While this number may vary, the output will be similar to this:

您将在终端窗口中收到输出,该输出将使您知道版本号。 尽管此数字可能有所不同,但输出将类似于以下内容:


   
     
     
     
     
Output
Python 3.6.7

To manage software packages for Python, let’s install pip, a tool that will install and manage programming packages we may want to use in our development projects. You can learn more about modules or packages that you can install with pip by reading “How To Import Modules in Python 3.”

要管理Python软件包,让我们安装pip ,该工具将安装和管理我们可能要在开发项目中使用的编程软件包。 通过阅读“ 如何在Python 3中导入模块 ”,可以了解有关可以通过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

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

Once Python is set up, and pip and other tools are installed, we can set up a virtual environment for our development projects.

设置Python,安装pip和其他工具后,我们可以为开发项目设置虚拟环境。

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

Virtual environments enable you to have an isolated space on your server 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 and over how different versions of packages are handled. This is especially important when working with third-party packages.

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

You can set up as many Python programming environments as you want. Each environment is basically a directory or folder on your server that has a few scripts in it to make it act as an environment.

您可以根据需要设置任意数量的Python编程环境。 每个环境基本上都是服务器上的目录或文件夹,其中包含一些脚本以使其充当环境。

While there are a few ways to achieve a programming environment in Python, we’ll be using the venv module here, which is part of the standard Python 3 library. Let’s install venv by typing:

尽管有几种方法可以在Python中实现编程环境,但我们将在这里使用venv模块,该模块是标准Python 3库的一部分。 让我们通过输入以下内容来安装venv:

  • sudo apt install -y python3-venv

    须藤apt install -y python3-venv

With this installed, we are ready to create environments. Let’s either choose which directory we would like to put our 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:

一旦进入您希望环境存在的目录,就可以通过运行以下命令来创建环境:

  • python3.6 -m venv my_env

    python3.6 -m venv my_env

Essentially, pyvenv sets up a new directory that contains a few items which we can view with the ls command:

本质上, pyvenv设置了一个新目录,其中包含一些我们可以使用ls命令查看的项目:

  • ls my_env

    ls my_env

   
     
     
     
     
Output
bin include lib lib64 pyvenv.cfg share

Together, these files work to make sure that your projects are isolated 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 Wheels, a built-package format for Python that can speed up your software production by reducing the number of times you need to compile, will be in the Ubuntu 18.04 share directory.

这些文件一起工作,以确保您的项目与本地计算机的更广泛的上下文隔离开来,从而避免系统文件和项目文件混在一起。 这是进行版本控制并确保您的每个项目都可以访问所需的特定程序包的良好做法。 Python Wheels是Python的一种内置打包格式,可以通过减少所需的编译次数来加快软件生产,它位于Ubuntu 18.04 share目录中。

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

要使用此环境,您需要激活它,可以通过键入以下调用激活脚本的命令来实现:

  • source my_env/bin/activate

    源my_env / bin / activate

Your command prompt will now be prefixed with the name of your environment, in this case it is called my_env. Depending on what version of Linux you are running, your prefix may appear somewhat differently, but the name of your environment in parentheses should be the first thing you see on your line:

现在,您的命令提示符将以您的环境名称为前缀,在本例中,该名称为my_env 。 根据您所运行的Linux版本,您的前缀可能会有所不同,但是在括号中的环境名称应该是您在该行中首先看到的内容:

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当前处于活动状态,这意味着在此处创建程序时,它们将仅使用此特定环境的设置和程序包。

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

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

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

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

第3步-创建一个“ Hello,World”程序 (Step 3 — Creating a “Hello, World” Program)

Now that we have our virtual environment set up, let’s create a traditional “Hello, World!” program. This will let us test our environment and provides us with 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 nano and create a new file:

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

  • nano hello.py

    纳米hello.py

Once the text file opens up in the terminal window we’ll type out our program:

在终端窗口中打开文本文件后,我们将输入程序:

print("Hello, World!")

Exit nano by typing the CTRL and X keys, and when prompted to save the file press y.

通过输入CTRLX键退出nano,然后在提示您保存文件时,按y

Once you exit out of nano and return to your shell, let’s run the program:

一旦退出nano并返回外壳,让我们运行该程序:

  • python hello.py

    python hello.py

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

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


   
     
     
     
     
Output
Hello, World!

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

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

结论 (Conclusion)

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

恭喜你! 至此,您已经在Ubuntu Linux服务器上设置了Python 3编程环境,现在就可以开始编码项目了!

If you are using a local machine rather than a server, refer to the tutorial that is relevant to your operating system in our “How To Install and Set Up a Local Programming Environment for Python 3” series.

如果您使用的是本地计算机而不是服务器,请参阅“ 如何为Python 3安装和设置本地编程环境 ”系列中与您的操作系统相关的教程。

With your server ready for software development, you can continue to learn more about coding in Python by reading our free How To Code in Python 3 eBook, or consulting our Programming Project tutorials.

在准备好进行软件开发的服务器之后,您可以阅读免费的《 如何在Python 3中 编写 代码》电子书或咨询我们的《 编程项目》教程 ,继续学习有关Python编程的更多信息。

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

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