Using virtualenv with your flask app 9


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.

virtualenv directory tree in flask

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

Great!

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 hello.py script:

virtual\Scripts\python app\hello.py

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

static flask website

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



Leave a comment

Your email address will not be published. Required fields are marked *

9 thoughts on “Using virtualenv with your flask app

  • john

    Hi folks! So far everything has been great but I am stuck here on this step.
    PS C:\Users\jch01_000\mystuff\myblog> python –m venv virtual

    I get this error:
    C:\Users\jch01_000\Anaconda2\python.exe: can’t open file ‘ûm’: [Errno 2] No such file or directory

  • Daniyar

    for those who have python version 2.7 you have to type in command line “virtualenv virtual”
    the python –m venv virtual command should use those who have python 3

  • Niklas Rudemo

    I am using Mac OS 10.11.5 and python 3.5.1

    The virtualenv commands above does not work for me. Instead, I used the following:

    virtualenv -p /usr/local/bin/python3.5 flasktest

  • Anthony

    You are not using virtualenv, but the builtin Python module venv.
    Note: python -m venv virtual vs
    python -m virtualenv virtual

  • CUCM

    Doing this on the Raspberry Pi 3:

    Instead of “pip install virtualenv”:

    pi@raspberrypi:~/myblog $ sudo pip install virtualenv

    Instead of “python –m virtualenv virtual”:

    pi@raspberrypi:~/myblog $ virtualenv virtual

    When I looked in the virtual folder I didn’t have the same things as you so I needed to start the virtual environment and see where the python was:

    pi@raspberrypi:~/myblog $ cd virtual

    pi@raspberrypi:~/myblog/virtual $ source bin/activate

    (virtual) pi@raspberrypi:~/myblog/virtual $ which python
    /home/pi/myblog/virtual/bin/python

    Instead of “virtual\Scripts\pip install flask”:

    pi@raspberrypi:~/myblog/ $ virtual/bin/pip install flask

    Then this was the path where flask is installed on my raspberry pi:
    /myblog/virtual/lib/python2.7/site-packages/flask

    Instead of “virtual\Scripts\python app\hello.py”:

    pi@raspberrypi:~/myblog/ $ virtual/bin/python app/hello.py

  • Chhuon Sahakthun

    Hi,
    I can not complete your 2 steps:
    >> pip install virtualenv
    >> python –m virtualenv virtual

    This is my result from the terminal window:
    —————————————————————————————————————————————————————
    Chhuon-Sahak-Thun:desktop kun$ pip install virtualenv
    Requirement already satisfied: virtualenv in /Users/kun/Library/Enthought/Canopy_32bit/User/lib/python2.7/site-packages
    Chhuon-Sahak-Thun:desktop kun$ python –m virtualenv virtual
    /Users/kun/Library/Enthought/Canopy_32bit/User/Resources/Python.app/Contents/MacOS/Python: can’t open file ‘–m’: [Errno 2] No such file or directory
    Chhuon-Sahak-Thun:desktop kun$
    —————————————————————————————————————————————————————
    Please help !!!