In this tutorial, we will install flask in a virtual environment. There are a number of advantages of working in a virtual environment -
- Firstly, we can set different configurations for each virtual environment. The packages installed in the virtual environment are not accessible outside it. It guarantees there are no conflicts with the global interpreter.
- To install new packages, virtual environments don’t require root privileges.
Let’s start by creating a virtual environment -
To check if
virtualenv is correctly installed, type -
At the time this post was written, the output was -
Now let’s make a directory
flask-application where we will store our project.
It time to create to virtual environment
flask-env, where we will install flask.
You will see the following output on the terminal -
We have successfully created our virtual environment, now let’s activate it using the command below -
You will now notice that on left side of your command prompt,
(flask-env) appears. So now, when you install a package, it will only be accessible from the virtual environment. When you exit the virtual environment, you can no longer access any package you install inside the virtual environment. It is time to install flask. We will use pip to install the package -
Let’s write a hello.py script which prints
"Hello World!" when we open
http://127.0.0.1:5000/ in the browser -
In the browser, open the url
http://127.0.0.1:5000/. The result looks like this -
To deactivate the virtual environment, type the below line in the command prompt -