Installing Flask

Note: Before following along make sure you have working Python installation and virtualenv package installed on your system. To learn how to install Python and virtualenv click here.

Creating Virtual Environment

A virtual environment is an isolated copy of Python installation where we can install packages without affecting the global Python installation. Create a new directory named flask_app. This directory will host our Flask application.

Change your current working directory to flask_app using the cd command.

The next step is to create a virtual environment inside flask_app directory using the virtualenv command.

After executing the above command, you should have a directory named env inside flask_app directory. The env directory constitutes a separate Python installation. It contains all the executable scripts just like an ordinary Python installation. To use this virtual environment, you have to first activate it.

To activate virtual environment in Linux and Mac OS, enter the following command.

Windows users can activate virtual environment by entering the following command.

Notice the virtual environment name inside the parentheses before the prompt string i.e (env). This indicates that our virtual environment is up and running. Packages you install from now on will only be available inside this virtual environment.

Activating virtual environment changes the PATH environment variable temporarily. So if you type python in the terminal, Python interpreter that resides in the virtual environment i.e env directory will be invoked instead of the global Python interpreter.

Once you are done working with the virtual environment you have to deactivate it using the deactivate command.

This command makes the global Python interpreter available again.

Installing Flask

To install Flask inside virtual environment enter the following command.

You can verify whether installation succeeds or not, by invoking Python interpreter and importing Flask.

If no error appears, that means Flask installation was successful.

Leave a Comment

%d bloggers like this: