Installing Flask
Last updated on July 27, 2020
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.
1 2 | overiq@vm:~$ mkdir flask_app
overiq@vm:~$
|
Change your current working directory to flask_app
using the cd
command.
1 2 | overiq@vm:~$ cd flask_app/
overiq@vm:~/flask_app$
|
The next step is to create a virtual environment inside flask_app
directory using the virtualenv
command.
1 2 3 4 5 6 | overiq@vm:~/flask_app$ virtualenv env
Using base prefix '/usr'
New python executable in /home/overiq/flask_app/env/bin/python3
Also creating executable in /home/overiq/flask_app/env/bin/python
Installing setuptools, pip, wheel...done.
overiq@vm:~/flask_app$
|
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.
1 2 | overiq@-vm:~/flask_app$ source env/bin/activate
(env) overiq@vm:~/flask_app$
|
Windows users can activate virtual environment by entering the following command.
1 2 | C:\Users\overiq\flask_app>env\Scripts\activate
(env) C:\Users\overiq\flask_app>
|
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.
1 2 | (env) overiq@vm:~/flask_app$ deactivate
overiq@vm:~/flask_app$
|
This command makes the global Python interpreter available again.
Installing Flask #
To install Flask inside virtual environment enter the following command.
(env) overiq@vm:~/flask_app$ pip install flask
You can verify whether installation succeeds or not, by invoking Python interpreter and importing Flask.
1 2 3 4 5 6 7 8 9 10 | (env) overiq@vm:~/flask_app$ python
Python 3.5.2 (default, Nov 17 2016, 17:05:23)
[GCC 5.4.0 20160609] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> import flask
>>>
>>> flask.__version__
'0.12.2'
>>>
|
If no error appears, that means Flask installation was successful.
Load Comments