Using a virtual environment with your flask app

Great job! Your website is now ready to deploy to an online server.

However, deploying a flask application that has been built with your main Python installation can be a bad idea sometimes. Furthermore, it is not advisable to even start creating a web app using your main Python installation. Because you use Python for many other things, there can be a lot going on in that installation.

Therefore, it would be good to have a clean environment that does not interfere with your main Python installation. The hero here is the venv library. Venv is a Python standard library that creates a virtual environment with an isolated Python installation. That installation will serve only for our flask web application, and not any other purposes. That allows us to deploy a clean application to the online server. venv comes shipped with Python, so you don’t need to install it.

Creating a virtual environment is the first thing you should do before writing any code. In this tutorial, I felt it would be better not to confuse you with that, and I decided to keep things plain and simple so that you could focus on the application instead. Now, you’re mature enough to use a virtual environment for your web applications. So, next time you build a flask application, you should create a virtual environment before you write any file. And below you will learn how to create one.

You can then create a virtual environment. The virtual environment files will be generated inside a folder that is at the same directory level with the folder where your app files are. It’s a good idea to create another folder (e.g., myblog) and put the app folder inside it. Then, open a  terminal/command line while you are in the myblog folder and type this:

python –m venv virtual

So, we’re using Python and the venv standard library to create a folder named virtual with all the necessary virtual environment files. 

If you explore the Scripts' folder, you will find a python.exe file. That means you have a Python installation in your virtual environment. And there is also a pip.exe which means you have the pip library with you as well and you can use it to install other libraries for your isolated Python.

And of course, we need to install our flask library if we want to run our website using the Python installation of our virtual environment. For that, you need to use your isolated pip. So, while you are inside the main myblog folder, you can type this in the command line:

virtual\Scripts\pip install flask

That should install Flask under virtual\Lib\site-packages.


Let’s now run our website using our virtual environment. For that, while you are in the myblog folder, you need to point to the isolated Python and then to the script:

virtual\Scripts\python app\

That will show the same website on localhost:5000 as it did before.

Now we are ready to throw this beautiful website to the online cloud.

Next Lecture
Recommended Course

Python Mega Course: Learn Python in 60 Days, Build 20 Apps
Learn Python on Udemy completely in 60 days or less by building 20 real-world applications from web development to data science.