Python how to install flask

Installation¶

We recommend using the latest version of Python. Flask supports Python 3.8 and newer.

Dependencies¶

These distributions will be installed automatically when installing Flask.

  • Werkzeug implements WSGI, the standard Python interface between applications and servers.
  • Jinja is a template language that renders the pages your application serves.
  • MarkupSafe comes with Jinja. It escapes untrusted input when rendering templates to avoid injection attacks.
  • ItsDangerous securely signs data to ensure its integrity. This is used to protect Flask’s session cookie.
  • Click is a framework for writing command line applications. It provides the flask command and allows adding custom management commands.
  • Blinker provides support for Signals .

Optional dependencies¶

These distributions will not be installed automatically. Flask will detect and use them if you install them.

  • python-dotenv enables support for Environment Variables From dotenv when running flask commands.
  • Watchdog provides a faster, more efficient reloader for the development server.

greenlet¶

You may choose to use gevent or eventlet with your application. In this case, greenlet>=1.0 is required. When using PyPy, PyPy>=7.3.7 is required.

These are not minimum supported versions, they only indicate the first versions that added necessary features. You should use the latest versions of each.

Virtual environments¶

Use a virtual environment to manage the dependencies for your project, both in development and in production.

Читайте также:  Array object example in php

What problem does a virtual environment solve? The more Python projects you have, the more likely it is that you need to work with different versions of Python libraries, or even Python itself. Newer versions of libraries for one project can break compatibility in another project.

Virtual environments are independent groups of Python libraries, one for each project. Packages installed for one project will not affect other projects or the operating system’s packages.

Python comes bundled with the venv module to create virtual environments.

Create an environment¶

Create a project folder and a .venv folder within:

$ mkdir myproject $ cd myproject $ python3 -m venv .venv
> mkdir myproject > cd myproject > py -3 -m venv .venv

Activate the environment¶

Before you work on your project, activate the corresponding environment:

Your shell prompt will change to show the name of the activated environment.

Install Flask¶

Within the activated environment, use the following command to install Flask:

Flask is now installed. Check out the Quickstart or go to the Documentation Overview .

Logo

Contents

Источник

Installation¶

We recommend using the latest version of Python. Flask supports Python 3.7 and newer.

Dependencies¶

These distributions will be installed automatically when installing Flask.

  • Werkzeug implements WSGI, the standard Python interface between applications and servers.
  • Jinja is a template language that renders the pages your application serves.
  • MarkupSafe comes with Jinja. It escapes untrusted input when rendering templates to avoid injection attacks.
  • ItsDangerous securely signs data to ensure its integrity. This is used to protect Flask’s session cookie.
  • Click is a framework for writing command line applications. It provides the flask command and allows adding custom management commands.

Optional dependencies¶

These distributions will not be installed automatically. Flask will detect and use them if you install them.

  • Blinker provides support for Signals .
  • python-dotenv enables support for Environment Variables From dotenv when running flask commands.
  • Watchdog provides a faster, more efficient reloader for the development server.

greenlet¶

You may choose to use gevent or eventlet with your application. In this case, greenlet>=1.0 is required. When using PyPy, PyPy>=7.3.7 is required.

These are not minimum supported versions, they only indicate the first versions that added necessary features. You should use the latest versions of each.

Virtual environments¶

Use a virtual environment to manage the dependencies for your project, both in development and in production.

What problem does a virtual environment solve? The more Python projects you have, the more likely it is that you need to work with different versions of Python libraries, or even Python itself. Newer versions of libraries for one project can break compatibility in another project.

Virtual environments are independent groups of Python libraries, one for each project. Packages installed for one project will not affect other projects or the operating system’s packages.

Python comes bundled with the venv module to create virtual environments.

Create an environment¶

Create a project folder and a .venv folder within:

$ mkdir myproject $ cd myproject $ python3 -m venv .venv
> mkdir myproject > cd myproject > py -3 -m venv .venv

Activate the environment¶

Before you work on your project, activate the corresponding environment:

Your shell prompt will change to show the name of the activated environment.

Install Flask¶

Within the activated environment, use the following command to install Flask:

Flask is now installed. Check out the Quickstart or go to the Documentation Overview .

Logo

Contents

Источник

Installation¶

Flask depends on some external libraries, like Werkzeug and Jinja2. Werkzeug is a toolkit for WSGI, the standard Python interface between web applications and a variety of servers for both development and deployment. Jinja2 renders templates.

So how do you get all that on your computer quickly? There are many ways you could do that, but the most kick-ass method is virtualenv, so let’s have a look at that first.

You will need Python 2.6 or newer to get started, so be sure to have an up-to-date Python 2.x installation. For using Flask with Python 3 have a look at Python 3 Support .

virtualenv¶

Virtualenv is probably what you want to use during development, and if you have shell access to your production machines, you’ll probably want to use it there, too.

What problem does virtualenv solve? If you like Python as much as I do, chances are you want to use it for other projects besides Flask-based web applications. But the more projects you have, the more likely it is that you will be working with different versions of Python itself, or at least different versions of Python libraries. Let’s face it: quite often libraries break backwards compatibility, and it’s unlikely that any serious application will have zero dependencies. So what do you do if two or more of your projects have conflicting dependencies?

Virtualenv to the rescue! Virtualenv enables multiple side-by-side installations of Python, one for each project. It doesn’t actually install separate copies of Python, but it does provide a clever way to keep different project environments isolated. Let’s see how virtualenv works.

If you are on Mac OS X or Linux, chances are that one of the following two commands will work for you:

$ sudo easy_install virtualenv
$ sudo pip install virtualenv

One of these will probably install virtualenv on your system. Maybe it’s even in your package manager. If you use Ubuntu, try:

$ sudo apt-get install python-virtualenv

If you are on Windows and don’t have the easy_install command, you must install it first. Check the pip and setuptools on Windows section for more information about how to do that. Once you have it installed, run the same commands as above, but without the sudo prefix.

Once you have virtualenv installed, just fire up a shell and create your own environment. I usually create a project folder and a venv folder within:

$ mkdir myproject $ cd myproject $ virtualenv venv New python executable in venv/bin/python Installing setuptools, pip. done.

Now, whenever you want to work on a project, you only have to activate the corresponding environment. On OS X and Linux, do the following:

If you are a Windows user, the following command is for you:

Either way, you should now be using your virtualenv (notice how the prompt of your shell has changed to show the active environment).

And if you want to go back to the real world, use the following command:

After doing this, the prompt of your shell should be as familar as before.

Now, let’s move on. Enter the following command to get Flask activated in your virtualenv:

A few seconds later and you are good to go.

System-Wide Installation¶

This is possible as well, though I do not recommend it. Just run pip with root privileges:

(On Windows systems, run it in a command-prompt window with administrator privileges, and leave out sudo .)

Living on the Edge¶

If you want to work with the latest version of Flask, there are two ways: you can either let pip pull in the development version, or you can tell it to operate on a git checkout. Either way, virtualenv is recommended.

Get the git checkout in a new virtualenv and run in development mode:

$ git clone http://github.com/mitsuhiko/flask.git Initialized empty Git repository in ~/dev/flask/.git/ $ cd flask $ virtualenv venv New python executable in venv/bin/python Installing setuptools, pip. done. $ . venv/bin/activate $ python setup.py develop . Finished processing dependencies for Flask

This will pull in the dependencies and activate the git head as the current version inside the virtualenv. Then all you have to do is run git pull origin to update to the latest version.

To just get the development version without git, do this instead:

$ mkdir flask $ cd flask $ virtualenv venv $ . venv/bin/activate New python executable in venv/bin/python Installing setuptools, pip. done. $ pip install Flask==dev . Finished processing dependencies for Flask==dev

pip and setuptools on Windows¶

Sometimes getting the standard “Python packaging tools” like pip, setuptools and virtualenv can be a little trickier, but nothing very hard. The two crucial packages you will need are setuptools and pip — these will let you install anything else (like virtualenv). Fortunately there are two “bootstrap scripts” you can run to install either.

If you don’t currently have either, then get-pip.py will install both for you (you won’t need to run ez_setup.py).

To install the latest setuptools, you can use its bootstrap file:

Either should be double-clickable once you download them. If you already have pip, you can upgrade them by running:

> pip install --upgrade pip setuptools

Most often, once you pull up a command prompt you want to be able to type pip and python which will run those things, but this might not automatically happen on Windows, because it doesn’t know where those executables are (give either a try!).

To fix this, you should be able to navigate to your Python install directory (e.g C:\Python27 ), then go to Tools , then Scripts ; then find the win_add2path.py file and run that. Open a new Command Prompt and check that you can now just type python to bring up the interpreter.

Finally, to install virtualenv, you can simply run:

Then you can be off on your way following the installation instructions above.

Источник

Оцените статью