Home Backend Development Python Tutorial Python的Flask框架的简介和安装方法

Python的Flask框架的简介和安装方法

Jun 10, 2016 pm 03:07 PM
flask Install Introduction

请在开始使用 Flask 之前阅读本文。也希望本文能够回答关于 Flask 项目的初衷以及目标,以及 flask 适用的场景(情境)等问题。

什么是 “微”?
“微” (“Micro”) 并不是意味着把整个 Web 应用放入到一个 Python 文件,尽管确实可以这么做。当然“微” (“Micro”) 也不是意味 Flask 的功能上是不足的。微框架中的 “微” (“Micro”) 是指 Flask 旨在保持代码简洁且易于扩展。Flask 不会为你做太多的选择,例如选择什么样的数据库。Flask 为你做的是很容易修改的,比如选择什么样的模版引擎。其它的一切取决于你,因此 Flask 能满足你所需要的。

默认情况下,Flask 并不包含数据库抽象层,表单验证或者任何其它现有的库( Django )能够处理的。相反,Flask 支持扩展,这些扩展能够添加功能到你的应用,像是 Flask 本身实现的一样。众多的扩展提供了数据库集成,表单验证,上传处理,多种开放的认证技术等功能。Flask 可能是“微”型的,但是已经能够在各种各样的需求中生产使用。

配置和约定
Flask 有许多带有合理默认值的配置项,也遵循一些惯例。例如:按惯例,模板和静态文件存储在应用 Python 源代码树下的子目录中,而这是可以改变的,你通常不必这么做,尤其是在刚开始的时候。

与 Flask 共同成长
一旦你的 Flask 项目搭建以及运行起来,你会发现在社区中有大量可用的扩展集成到你的生产环境项目中来。Flask 核心团队会审阅这些扩展,确保经过验证过的扩展在未来版本中仍能使用。

随着你的代码库的增长,你能够自由地为你的项目做出恰当的设计决定。Flask 会继续尽 Python 的可能提供一个简单的粘合层。你可以在 SQLAlchemy 或者其它数据库工具中实现高级模式,适当的时候引入非关系型数据持久化,使用框架无关的 WSGI 工具,WSGI 是 Python 的 web 接口。

安装
Flask 依赖两个外部库, Werkzeug 和 Jinja2。Werkzeug 是一个 WSGI 工具集,它是 web 应用程序和用于开发和部署的服务器之间的标准接口。Jinja2 负责渲染模板。

因此怎样才能快速地安装这一切了?你有很多种方法去安装,但是最简单粗暴的方式就是 virtualenv, 让我们首先来看看它。

virtualenv
也许 Virtualenv 是你在开发中最愿意使用的,如果你在生产机器上有 shell 权限的时候,你也会愿意用上 virtualenv。

virtualenv 解决了什么问题?如果你像我一样喜欢 Python 的话,有很多机会在基于 Flask 的 web 应用外的其它项目上使用 Python。 然而项目越多,越有可能在不同版本的 python,或者至少在不同 python 库的版本上工作。 我们需要面对这样的事实:库破坏向后兼容性的情况相当常见,而且零依赖的正式应用也不大可能存在。 如此,当你的项目中的两个或更多出现依赖性冲突,你会怎么做?

Virtualenv 的出现解决这一切!Virtualenv 能够允许多个不同版本的 Python 安装,每一个服务于各自的项目。 它实际上并没有安装独立的 Python 副本,只是提供了一种方式使得环境保持独立。让我们见识下 virtualenv 怎么工作的。

如果你在 Mac OS X 或 Linux下,下面两条命令可能会适用:

$ sudo easy_install virtualenv
Copy after login

或者更好的:

$ sudo pip install virtualenv
Copy after login

上述的命令会在你的系统中安装 virtualenv。它甚至可能会出现在包管理器中。如果你使用 Ubuntu ,请尝试:

$ sudo apt-get install python-virtualenv
Copy after login

如果是在 Windows 下并且没有安装 easy_install 命令,你首先必须安装 easy_install 。 一旦安装好 easy_install , 运行上述的命令,但是要去掉 sudo 前缀。

一旦成功安装 virtualenv,运行 shell 创建自己的环境。我通常会创建一个项目文件夹,其下创建 venv 文件夹:

$ mkdir myproject
$ cd myproject
$ virtualenv venv
Copy after login

New python executable in venv/bin/python
Installing distribute............done.
Copy after login
Copy after login
Copy after login

现在,只要你想要在某个项目上工作,只要激活相应的环境。在 OS X 和 Linux 下,按如下做:

$ . venv/bin/activate
Copy after login

如果你是个 Windows 用户,下面的命令行是为你准备的:

$ venv\scripts\activate
Copy after login

无论哪种方式,你现在能够使用你的 virtualenv (注意你的 shell 提示符显示的是活动的环境)。

现在你只需要键入以下的命令来激活你的 virtualenv 中的 Flask:

$ pip install Flask
Copy after login

几秒后,一切就为你准备就绪。

全局安装
这样也是可能的,尽管我不推荐。只需要以 root 权限运行 pip:

$ sudo pip install Flask
Copy after login

(在 Windows 系统上,在管理员权限的命令提示符中运行这条命令,不需要 sudo。)

体验最新的 Flask (Living on the Edge)
如果你想要用最新版的 Flask 干活,这里有两种方式:你可以使用 pip 拉取开发版本, 或让它操作一个 git checkout。无论哪种方式,依然推荐使用 virtualenv。

在一个新的 virtualenv 上获取一个 git checkout,在开发模式下运行:

$ git clone http://github.com/mitsuhiko/flask.git
Copy after login

Initialized empty Git repository in ~/dev/flask/.git/
Copy after login

$ cd flask
$ virtualenv venv --distribute
Copy after login

New python executable in venv/bin/python
Installing distribute............done.
Copy after login
Copy after login
Copy after login

$ . venv/bin/activate
$ python setup.py develop
Copy after login

...
Finished processing dependencies for Flask
Copy after login

这会拉取依赖关系并激活 git head 作为 virtualenv 中的当前版本。然后你只需要执行 git pull origin 来升级到最新版本。

没有 git 下获取最新的开发版本,需要这样做:

$ mkdir flask
$ cd flask
$ virtualenv venv --distribute
$ . venv/bin/activate
Copy after login

New python executable in venv/bin/python
Installing distribute............done.
Copy after login
Copy after login
Copy after login

$ pip install Flask==dev
Copy after login

...
Finished processing dependencies for Flask==dev
Copy after login

Windows 下的 pip 和 distribute
在 Windows 系统下,安装 easy_install 有些棘手,但是仍然很简单。最简单的方式是下载 distribute_setup.py 文件接着运行它。运行这个文件最简单的方式就是打开下载文件夹接着双击这个文件。

接着,把 Python 的 Scripts 文件夹添加到 PATH 环境变量来,这样 easy_install 命令和其它 Python 脚本就加入到了命令行自动搜索的路径。做法是:右键单击桌面上或是“开始”菜单中的“我的电脑”图标,选择“属性”, 然后单击“高级系统设置”(在 Windows XP 中,单击“高级”选项卡),然后单击“环境变量”按钮, 最后双击“系统变量”栏中的“Path”变量,并加入你的 Python 解释器的 Scripts 文件夹。 确保你用分号把它和现有的值分隔开。假设你使用 Python 2.7 且为默认目录,添加下面的值:

;C:\Python27\Scripts
Copy after login

这样就完成了!为了检测是否正常工作,打开命令提示符执行 easy_install。在 Windows Vista 或者 Windows 7 下如果开启了用户账户控制,它应该提示需要管理员权限。

现在已经安装好 easy_install,你能使用它来安装 pip:

> easy_install pip
Copy after login

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to install Android apps on Linux? How to install Android apps on Linux? Mar 19, 2024 am 11:15 AM

Installing Android applications on Linux has always been a concern for many users. Especially for Linux users who like to use Android applications, it is very important to master how to install Android applications on Linux systems. Although running Android applications directly on Linux is not as simple as on the Android platform, by using emulators or third-party tools, we can still happily enjoy Android applications on Linux. The following will introduce how to install Android applications on Linux systems.

How to install Podman on Ubuntu 24.04 How to install Podman on Ubuntu 24.04 Mar 22, 2024 am 11:26 AM

If you have used Docker, you must understand daemons, containers, and their functions. A daemon is a service that runs in the background when a container is already in use in any system. Podman is a free management tool for managing and creating containers without relying on any daemon such as Docker. Therefore, it has advantages in managing containers without the need for long-term backend services. Additionally, Podman does not require root-level permissions to be used. This guide discusses in detail how to install Podman on Ubuntu24. To update the system, we first need to update the system and open the Terminal shell of Ubuntu24. During both installation and upgrade processes, we need to use the command line. a simple

How to Install and Run the Ubuntu Notes App on Ubuntu 24.04 How to Install and Run the Ubuntu Notes App on Ubuntu 24.04 Mar 22, 2024 pm 04:40 PM

While studying in high school, some students take very clear and accurate notes, taking more notes than others in the same class. For some, note-taking is a hobby, while for others, it is a necessity when they easily forget small information about anything important. Microsoft's NTFS application is particularly useful for students who wish to save important notes beyond regular lectures. In this article, we will describe the installation of Ubuntu applications on Ubuntu24. Updating the Ubuntu System Before installing the Ubuntu installer, on Ubuntu24 we need to ensure that the newly configured system has been updated. We can use the most famous "a" in Ubuntu system

Detailed steps to install Go language on Win7 computer Detailed steps to install Go language on Win7 computer Mar 27, 2024 pm 02:00 PM

Detailed steps to install Go language on Win7 computer Go (also known as Golang) is an open source programming language developed by Google. It is simple, efficient and has excellent concurrency performance. It is suitable for the development of cloud services, network applications and back-end systems. . Installing the Go language on a Win7 computer allows you to quickly get started with the language and start writing Go programs. The following will introduce in detail the steps to install the Go language on a Win7 computer, and attach specific code examples. Step 1: Download the Go language installation package and visit the Go official website

How to install Go language under Win7 system? How to install Go language under Win7 system? Mar 27, 2024 pm 01:42 PM

Installing Go language under Win7 system is a relatively simple operation. Just follow the following steps to successfully install it. The following will introduce in detail how to install Go language under Win7 system. Step 1: Download the Go language installation package. First, open the Go language official website (https://golang.org/) and enter the download page. On the download page, select the installation package version compatible with Win7 system to download. Click the Download button and wait for the installation package to download. Step 2: Install Go language

Python ORM Performance Benchmark: Comparing Different ORM Frameworks Python ORM Performance Benchmark: Comparing Different ORM Frameworks Mar 18, 2024 am 09:10 AM

Object-relational mapping (ORM) frameworks play a vital role in python development, they simplify data access and management by building a bridge between object and relational databases. In order to evaluate the performance of different ORM frameworks, this article will benchmark against the following popular frameworks: sqlAlchemyPeeweeDjangoORMPonyORMTortoiseORM Test Method The benchmarking uses a SQLite database containing 1 million records. The test performed the following operations on the database: Insert: Insert 10,000 new records into the table Read: Read all records in the table Update: Update a single field for all records in the table Delete: Delete all records in the table Each operation

Essential PHP programs: Install these to run smoothly! Essential PHP programs: Install these to run smoothly! Mar 27, 2024 pm 05:54 PM

Essential PHP programs: Install these to run smoothly! PHP is a popular server-side scripting language that is widely used to develop web applications. To successfully run a PHP program, you first need to install some necessary software and tools on the server. In this article, we will introduce the software and tools that must be installed, along with specific code examples to help you run PHP programs smoothly. 1. PHP interpreter The core of the PHP program is the PHP interpreter, which is responsible for parsing and executing PHP code. To install the PHP interpreter, you can follow

PHP FFmpeg extension installation guide: easy-to-follow tutorial PHP FFmpeg extension installation guide: easy-to-follow tutorial Mar 28, 2024 pm 02:17 PM

PHPFFmpeg Extension Installation Guide: Simple and easy-to-understand tutorial In the process of website development, sometimes we need to process various multimedia files, such as audio, video, etc. FFmpeg is a powerful multimedia processing tool that can process audio, video and other formats, and supports various transcoding, cutting and other operations. The PHPFFmpeg extension is an extension library that calls FFmpeg functions in PHP. It can be used to process multimedia files easily. Below we will introduce PHPF in detail

See all articles