Your website is ready to deploy to an online server or to the cloud if you want to talk more futuristic.
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 in that installation.
Therefore, it would be good to have a clean environment that does not have to do with your main Python installation. The hero here is the virtualenv library. virtualenv is a Python library that creates a virtual environment with an isolated Python installation. That installation will serve only to our flask web application, and not any other purposes. That will allow us to deploy a clean application to the online server.
Creating a virtual environment is the first thing you should do before writing any code. In this tutorial I didn’t want 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.
To create a virtual environment for Python, you should first install the virtualenv Python library for your current Python installation. That is the tool that will create a virtual environment in our local computer. To install virtualenv, type this in the terminal/command line.
pip install virtualenv
Once virtualenv is installed, you can then create a virtual environment. The virtual environment files should 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 in the myblog folder and type this:
python –m virtualenv virtual
So, we’re using Python and the
virtualenv library to create a folder named virtual with all the necessary virtual environment files. Your directory tree should look like this now.
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
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.