Python 是一种灵活多功能的编程语言,适用于多种使用场景,在脚本编写、自动化、数据分析、机器学习和后端开发方面表现出色。Python 开发团队于 1991 年首次发布该语言时,灵感来源于英国喜剧团体 Monty Python,旨在打造一种使用起来充满乐趣的编程语言。Python 3 是该语言的最新版本,被认为是 Python 的未来

本教程将帮助你在远程服务器或本地计算机上设置 Python 3 编程环境。如果你已经安装了 Python 3、pip 和 venv,请随意跳到下一章!


先决条件

本教程将基于 Linux 或 Unix-like (nix) 系统,并使用*命令行或终端环境**。macOS 和 Windows 的 PowerShell 程序应该也能达到类似的效果。


步骤 1 — 安装 Python 3

许多操作系统都预装了 Python 3。你可以通过打开终端窗口并输入以下命令来检查是否已安装 Python 3:

Bash
python3 -V

你将在终端窗口中收到输出,告诉你版本号。虽然这个数字可能有所不同,但输出将与此类似:

Output
Python3.7.2

如果你收到其他输出,可以在网络浏览器中访问 python.org,按照说明下载 Python 3 并将其安装到你的机器上。

一旦你能够输入上面的 python3 -V 命令并收到显示你的计算机 Python 版本号的输出,你就可以继续了。


步骤 2 — 安装 pip

为了管理 Python 的软件包,我们来安装 pip,这是一个用于安装和管理我们可能希望在开发项目中使用的编程包的工具。

如果你是从 python.org 下载的 Python,那么 pip 应该已经安装好了。如果你在 Ubuntu 或 Debian 服务器或计算机上,可以通过输入以下命令下载 pip:

Bash
sudo apt install -y python3-pip

现在你已经安装了 pip,可以使用以下命令下载 Python 包:

Bash
pip3 install package_name

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

为了确保我们的编程环境拥有强大的设置,还需要安装一些额外的软件包和开发工具:

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

一旦 Python 设置完毕,pip 和其他工具安装完成,我们就可以为我们的开发项目设置一个虚拟环境了。


步骤 3 — 设置虚拟环境

虚拟环境使你能够在服务器上为 Python 项目提供一个隔离的空间,确保你的每个项目都可以拥有自己的一套依赖项,而不会干扰你的其他项目。

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

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

虽然有几种方法可以在 Python 中实现编程环境,但我们这里将使用 venv 模块,它是标准 Python 3 库的一部分。

如果你是通过 python.org 提供的安装程序安装的 Python,那么 venv 应该已经准备就绪。

要在 Ubuntu 或 Debian 服务器或机器上安装 venv,可以使用以下命令:

Bash
sudo apt install -y python3-venv

安装 venv 后,我们现在可以创建环境了。让我们选择要放置 Python 编程环境的目录,或者使用 mkdir 创建一个新目录,例如:

Bash
mkdir environments
cd environments

一旦你进入了你希望环境所在的目录,你就可以创建一个环境了。你应该使用安装在你机器上的 Python 版本作为命令的第一部分(你输入 python -V 时收到的输出)。如果该版本是 Python 3.6.3,你可以输入以下内容:

Bash
python3.6 -m venv my_env

如果你的计算机安装的是 Python 3.7.3,则使用以下命令:

Bash
python3.7 -m venv my_env

Windows 机器可能允许你完全删除版本号:

Bash
python -m venv my_env

运行相应的命令后,你可以通过继续来验证环境是否已设置。

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

Bash
ls my_env
Output
bin include lib lib64 pyvenv.cfg share

这些文件共同确保你的项目与本地机器的更广泛上下文隔离,从而使系统文件和项目文件不会混淆。这是版本控制的良好实践,并确保你的每个项目都能访问其所需的特定软件包。Python Wheels 是一种 Python 的构建包格式,可以通过减少编译次数来加快软件生产速度,它将位于 Ubuntu 18.04 的 share 目录中。

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

Bash
source my_env/bin/activate

你的命令提示符现在将以你的环境名称为前缀,在本例中是 my_env。根据你运行的 Debian Linux 版本,你的前缀可能看起来有所不同,但括号中的环境名称应该是你在行中看到的第一件事:

(my_env) sammy@sammy:~/environments$

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

注意:在虚拟环境内,如果你愿意,可以使用 python 命令而不是 python3,以及 pip 命令而不是 pip3。如果你在环境外部的机器上使用 Python 3,则需要专门使用 python3pip3 命令。

按照这些步骤操作后,你的虚拟环境就可以使用了。


步骤 4 — 创建“Hello, World”程序

现在我们已经设置好了虚拟环境,让我们来创建一个传统的“Hello, World!”程序。这将允许我们测试我们的环境,并为我们提供一个更熟悉 Python 的机会,如果我们还不熟悉的话。

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

Bash
(my_env) sammy@sammy:~/environments$ nano hello.py

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

Python
print("Hello, World!")

通过按下 CTRL 和 X 键退出 nano,当提示保存文件时按 y

一旦你退出 nano 并返回到你的 shell,我们来运行程序:

Bash
(my_env) sammy@sammy:~/environments$ python hello.py

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

Output
Hello, World!

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


结论

至此,你已经在你的机器上设置了一个 Python 3 编程环境,现在可以开始一个编码项目了!

如果你想了解更多关于 Python 的信息,可以通过 do.co/python-book 下载我们免费的电子书 《How To Code in Python 3》


Python is a flexible and versatile programming language suitable for
many use cases, with strengths in scripting, automation, data analysis,
machine learning, and back-end development. First published in 1991 the
Python development team was inspired by the British comedy group
Monty Python to make a programming language that was fun to use.
Python 3 is the most current version of the language and is considered to
be the future of Python.
This tutorial will help get your remote server or local computer set up
with a Python 3 programming environment. If you already have Python
3 installed, along with pip and venv, feel free to move onto the next
chapter!
Prerequisites
This tutorial will be based on working with a Linux or Unix-like (*nix)
system and use of a command line or terminal environment. Both macOS
and specifically the PowerShell program of Windows should be able to
achieve similar results.
Step 1 — Installing Python 3
Many operating systems come with Python 3 already installed. You can
check to see whether you have Python 3 installed by opening up a
terminal window and typing the following:
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
Python3.7.2
If you received alternate output, you can navigate in a web browser to
python.org in order to download Python 3 and install it to your machine
by following the instructions.
Once you are able to type the python3 -V command above and
receive output that states your computer’s Python version number, you
are ready to continue.
Step 2 — Installing pip
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.
If you have downloaded Python from python.org, you should have pip
already installed. If you are on an Ubuntu or Debian server or computer,
you can download pip by typing the following:
sudo apt install -y python3-pip
Now that you have pip installed, you can download Python packages
with the following command:
pip3 install 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.
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
Once Python is set up, and pip and other tools are installed, we can set
up a virtual environment for our development projects.
Step 3 — 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.
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.
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.
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.
If you have installed Python with through the installer available from
python.org, you should have venv ready to go.
To install venv into an Ubuntu or Debian server or machine, you can
install it with the following:
sudo apt install -y python3-venv
With venv installed, we can now 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:
mkdir environments
cd environments
Once you are in the directory where you would like the environments
t o live, you can create an environment. You should use the version of
Python that is installed on your machine as the first part of the command
(the output you received when typing python -V). If that version was
Python 3.6.3, you can type the following:
python3.6 -m venv my_env
If, instead, your computer has Python 3.7.3
 installed, use the
following command:
python3.7 -m venv my_env
Windows machines may allow you to remove the version number
entirely:
python -m venv my_env
Once you run the appropriate command, you can verify that the
environment is set up be continuing.
Essentially, pyvenv sets up a new directory that contains a few items
which we can view with the ls command:
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.
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
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
o f Debian 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) sammy@sammy:~/environments$
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.
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.
After following these steps, your virtual environment is ready to use.
Step 4 — 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.
To do this, we’ll open up a command-line text editor such as nano and
create a new file:
(my_env) sammy@sammy:~/environments$ nano 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.
Once you exit out of nano and return to your shell, let’s run the
program:
(my_env) sammy@sammy:~/environments$ python hello.py
T h e hello.py program that you just created should cause your
terminal to produce the following output:
Output
Hello, World!
To leave the environment, simply type the command deactivate and
you will return to your original directory.
Conclusion
At this point you have a Python 3 programming environment set up on
your machine and you can now begin a coding project!
If you would like to learn more about Python, you can download our
free How To Code in Python 3 eBook via do.co/python-book.

最后修改: 2025年06月25日 星期三 10:50