0
点赞
收藏
分享

微信扫一扫

【测试开发学习历程】Python数据类型:字符串-str(上)

鲤鱼打个滚 03-29 08:00 阅读 5

Chapter03 Getting Started with FastAPI

12 Technical requirements

FastAPI+React全栈开发12 搭建FastAPI开发环境

For this chapter, you will need the following:

  • Python setup
  • Virtual environments
  • Code editor and plugins
  • Terminal
  • REST clients

对于这一章,你需要以下的东西:

  • Python设置
  • 虚拟环境
  • 代码编辑器和插件
  • 终端
  • REST客户端

Let’s take a look at them in more detail.

让我们更详细地看看它们。

Python setup

If you do not have Python installed, now is a good time to do so. Head over to the Python download site (https://www.python.org/downloads) and download the installer for your operating system. The Python website contains excellent documentation for all the major operating systems. In this book, I will be using the latest, version, which, at the time of writing, is 3.10.1. Make sure that you install or upgrade to one of the latest Python versions. FastAPI relies heavily on Python hints and annotations, so any version later than 3.6 should work. Another important thing to check is that the Python version that you have installed is reachable or, even better, the default version. You can check this by typing python in your terminal of choice and checking the version.

如果您没有安装Python,现在是安装Python的好时机。转到Python下载站点(https://www.python.org/downloads)并为您的操作系统下载安装程序。Python网站包含所有主要操作系统的优秀文档。在本书中,我将使用最新的版本,在撰写本文时,它是3.10.1。请确保安装或升级到最新的Python版本之一。FastAPI严重依赖于Python提示和注解,因此3.6以后的任何版本都可以使用。另一件需要检查的重要事情是,您安装的Python版本是否可访问,或者最好是默认版本。您可以通过在您选择的终端中输入python并检查版本来检查这一点。

I will not describe how to install Python on your platform since there are numberous guides online that certainly cover your setup. You can also install a data science-friendly environment such as Anaconda(https://www.anaconda.com/products/distribution) if that is something you are already using or are comfortable with.

我不会描述如何在您的平台上安装Python,因为网上有许多指南肯定涵盖了您的设置。您还可以安装一个数据科学友好的环境,如Anaconda(https://www.anaconda.com/products/distribution),如果您已经在使用或对它感到满意的话。

注意:这里我更推荐使用Anaconda配置Python环境,具体可以参考我的这篇博客:《用Anaconda创建Python指定版本的虚拟环境》 https://blog.csdn.net/qq_37703224/article/details/136666316

Virtual environments

We must create an application directory called FARM. Inside it, we will create a virtual environment. A virtual environment is an isolated Python environment that includes a copy of the Python interpreter, which we can use to install the desired packages and only the needed packages, in our case, FastAPI, the Uvicorn web server, and additional packages that we will use later, as well as FastAPI dependencies such as Pydantic.

我们必须创建一个名为FARM的应用程序目录。在里面,我们将创建一个虚拟环境。虚拟环境是一个独立的Python环境,包括Python解释器的副本,我们可以使用它来安装所需的包,并且只安装所需的包,在我们的例子中,FastAPI, Uvicorn web服务器,以及我们稍后将使用的其他包,以及FastAPI依赖项,如Pydantic。

You can think of a virtual environment as a directory tree of Python executable files and packages that allow us to have different versions of Python and the various third-paty packages together on a single machine, without them interfering with each other.

您可以将虚拟环境想象成Python可执行文件和包的目录树,它允许我们在一台机器上同时拥有不同版本的Python和各种第三方包,而不会相互干扰。

There are several excellent yet opinionated approaches to creating virtual environments such as pipenv(https://pipenv.pypa.io/en/latest/), pyenv(https://github.com/pyenv/pyenv), and others. I will stick to the simplest. After installing Python, you should head to your apps directory and, ina command prompt or your choice, run the following command.

创建虚拟环境有几种优秀但固执己见的方法,如pipenv(https://pipenv.pypa.io/en/latest/)、pyenv(https://github.com/pyenv/pyenv)等。我会坚持用最简单的。安装Python后,你应该进入你的apps目录,在命令提示符下运行以下命令。

python -m venv venv

This command should download a new Python executable and create a new directory named venv, it is the second argument in the command, so this can get a bit confusing. You can name it differently, but like many other things, this is a useful convention(readymade .gitignore files, for instance, often include venv to help you avoid putting the virtual envionment folder under version control). Now, we should check that we can activate the newly created environment. In the venv folder, navigate to the Scripts folder and type activate. The prompt should change and be prepended with the name of the active environment in parenthesis, that is $(venv).

这个命令应该下载一个新的Python可执行文件,并创建一个名为venv的新目录,它是命令中的第二个参数,所以这可能会让人有点困惑。您可以将其命名为不同的名称,但与许多其他内容一样,这是一个有用的约定(例如,现成的.gitignore文件通常包含venv,以帮助您避免将虚拟环境文件夹置于版本控制之下)。现在,我们应该检查是否可以激活新创建的环境。在venv文件夹中,导航到Scripts文件夹并键入activate。提示符应该改变,并在括号中加上活动环境的名称,即$(venv)。

You should also have Git installed. Head over to https://git-scm.com/downloads and just follow the instructions for your operating system.

您还应该安装Git。登录https://git-scm.com/downloads,按照操作系统的说明操作即可。

Apart from a Python environment and Git, you should have installed a shell program, if you are working on Linux or Mac, you should be fine. On Windows, you can use Windows, you can use Windows PowerShell or something such as Commander(https://cmder.app), a console emulator for Windows that has some nice features baked in.

除了Python环境和Git,你应该安装一个shell程序,如果你在Linux或Mac上工作,你应该很好。在Windows上,您可以使用Windows,也可以使用Windows PowerShell或Commander(https://cmder.app)之类的东西,这是一个Windows控制台模拟器,内置了一些不错的功能。

Code Editors

While there are many great code editors and integrated development environments(IDEs) for Python, I strongly recommend Visual Studio Code from Microsoft. Since its release in 2015, it has quickly become the most popular code editor, it is cross-platform, it provides a lot of integrated tools, such as an integrated terminal in which we can run our development server, it is pretty light weight, and it provides hundreds of plugins suitable for virtually any programming task that you may have to perform.

虽然有许多很棒的Python代码编辑器和集成开发环境(ide),但我强烈推荐微软的Visual Studio code。自2015年发布以来,它已经迅速成为最受欢迎的代码编辑器,它是跨平台的,它提供了很多集成工具,比如一个集成终端,我们可以在其中运行我们的开发服务器,它很轻,它提供了数百个插件,适用于几乎任何编程任务,你可能不得不执行。

Since we will be working with JavaScript, Python, React, and a bit of CSS for styling and running a couple of command-line processes along the way, I believe this is the easiest way to go.

因为我们将使用JavaScript、Python、React和一些CSS来进行样式化,并在此过程中运行一些命令行进程,所以我相信这是最简单的方法。

REST Clients

Finally, to be able to test our REST API, we need a REST client. Postman(https://www.postman.com/) is arguably the most sophiticated and customizable program for testing APIs, but there are several very viable alternatives. I prefer the simplicity of Insomnia(https://insomnia.rest/), a GUI REST client with a very clean and simple interface, ant HTTPie(https://httpie.io/), a command-line REST API client that allows us to quickly test our endpoints without leaving the shell. HTTPie provides other nice features such as an expressive and simple syntax, handling of forms and uploads, sessions, and so on. Moreover, HTTPie is probably the easiest REST client when it comes to installation, you can install hte Python version just like any other Python package, using pip or some other option, such as choco, apt(for Linux), brew, and so on.

最后,为了能够测试REST API,我们需要一个REST客户端。Postman(https://www.postman.com/)可以说是用于测试api的最复杂和最可定制的程序,但也有一些非常可行的替代方案。我更喜欢Insomnia(https://insomnia.rest/)和HTTPie(https://httpie.io/)的简单性,前者是一个GUI REST客户端,具有非常干净和简单的界面,后者是一个命令行REST API客户端,允许我们在不离开shell的情况下快速测试端点。HTTPie还提供了其他一些不错的特性,比如表达性强且简单的语法、表单和上传的处理、会话等等。此外,当涉及到安装时,HTTPie可能是最简单的REST客户端,您可以像安装任何其他Python包一样安装Python版本,使用pip或其他选项,如choco、apt(用于Linux)、brew等。

For our purposes, the easiest wa to install HTTPie is to activate the virtual environment that we will be using (cd into the venv directory, navigate to Lib/activate, and then activate it) and then insall HTTPie with pip, as follows.

出于我们的目的,安装HTTPie最简单的方法是激活我们将要使用的虚拟环境(cd进入venv目录,导航到Lib/activate,然后激活它),然后使用pip安装HTTPie,如下所示。

pip install httpie

Once it’s been installed, you can test HTTPie with the following command:

一旦它被安装,你可以用下面的命令测试HTTPie:

http GET "https://jsonplaceholder.typicode.com/todos/1"

If everything went well, you should have a pretty long output that starts with an HTTP/1.1 200 OK.

如果一切顺利,您应该有一个相当长的输出,以HTTP/1.1 200 OK开头。

In the previous command that we issued, you may be wondering what (venv) and the Greek letter Lambda mean. venv is just the name of our virtual environment and it means that it has been activated for the shell taht we are using (if you named it some other way, you would see a different name in parenthesis), while Lambda is the shell symbol used by Commander, my shell emulator. On Linux, Mac, or Windows PowerShell, you’ll get the standard > symbol. HTTPie makes it very easy to issue HTTP requests by simply adding POST for POST requests, payloads, form values, and so on.

在我们发布的上一个命令中,您可能想知道(venv)和希腊字母Lambda是什么意思。venv只是我们的虚拟环境的名称,它意味着它已经为我们正在使用的shell激活(如果您以其他方式命名它,您将在括号中看到不同的名称),而Lambda是shell模拟器Commander使用的shell符号。在Linux、Mac或Windows PowerShell上,你会得到标准的>符号。HTTPie通过简单地为POST请求、有效负载、表单值等添加POST,使发出HTTP请求变得非常容易。

Installing the necessary packages

After setting up the virtual environment, you should activate it and install the Python libraries required for our first simple application: FastAPI itself and Uviron.

在设置虚拟环境之后,您应该激活它并安装我们的第一个简单应用程序所需的Python库:FastAPI本身和Uviron。

FastAPI needs a server to run, by a server, I mean a pece of software specifically designed to serve web applications (or REST APIs!). FastAPI exposes an asynchronous server gateway interface (ASGI https://asgi.readthedocs.io/), compatible web application but doesn’t provide us with a built-in server, so a compatible Python solution is necessary.

FastAPI需要一个服务器来运行,这里的服务器指的是一个专门为web应用程序(或REST api)服务的软件。FastAPI公开了一个异步服务器网关接口(ASGI https://asgi.readthedocs.io/),兼容web应用程序,但没有为我们提供内置服务器,因此兼容的Python解决方案是必要的。

I will not get into the specifics of the ASGI specification, but the main takeaway is that it is an asynchronousinterface that enables async non-blocking applications, something that we want to make full use of our FastAPI capabilities. At the time of writing, the FastAPI documentation site lists three compatible Python ASGI compatible servers, Uvicorn, Hypercorn, and Daphne, but we will stick to the first one as it is the most widely used and is the recommended way to work with FastAPI. You can find lots of documentation online in case you get stuck, and it offers very high performance.

我不会深入讨论ASGI规范的细节,但主要的要点是,它是一个异步接口,支持异步非阻塞应用程序,我们希望充分利用我们的FastAPI功能。在撰写本文时,FastAPI文档网站列出了三个兼容的Python ASGI兼容服务器,Uvicorn, Hypercorn和Daphne,但我们将坚持使用第一个,因为它是最广泛使用的,并且是使用FastAPI的推荐方式。如果遇到困难,您可以在网上找到很多文档,而且它提供了非常高的性能。

To install our first two dependencies, make sure you are in your working directory with the desired virtual environment activated and install FastAPI and Uvicorn.

要安装前两个依赖项,请确保在工作目录中激活了所需的虚拟环境,并安装FastAPI和Uvicorn。

pip install fastapi uvicorn

Phew! This was a bit long, but now we have a decent Python coding environment that contains a shell, one or two REST clients, a great editor, and the coolest and hippest REST framework ready to rock. On the other hand, if you have ever developed a Django or Flask application, this should all be familiar groud.

唷!这有点长,但现在我们有了一个体面的Python编码环境,它包含一个shell、一两个REST客户端、一个很棒的编辑器,以及最酷、最时髦的REST框架。另一方面,如果您曾经开发过Django或Flask应用程序,那么这些内容应该都很熟悉。

Finally, let’s pick a folder or clone this book’s GitHub repository and activate a virtual environment(it is customary to create the environment in a folder named venv inside the working directory, but feel free to improvize and structure the code as you see fit). Later in this chapter, we will briefly discuss some options when it comes to structuring your FastAPI code, but for now, just make sure that you are in a folder and that your newly created virtual environment is activated.

最后,让我们选择一个文件夹或克隆本书的GitHub存储库,并激活一个虚拟环境(习惯上在工作目录内名为venv的文件夹中创建环境,但也可以随意修改和构建您认为合适的代码)。在本章的后面,我们将简要地讨论一些关于构建FastAPI代码的选项,但是现在,只要确保你在一个文件夹中,并且你新创建的虚拟环境是激活的。

举报

相关推荐

0 条评论