Project - How to Deploy an Image Classification Model using Flask

4 / 15

Creating a Virtual Environment

What is a Virtual Environment?

  • As the name suggests, it is an environment virtually created to isolate the projects.

  • This isolation helps to avoid discrepancies between the different versions of the libraries or packages as used by different projects in our system.

Why do we use a Virtual Environment?

  • Say, we have installed Flask Framework of version 1.1 some time ago, and have built an app(say App A) based on that.

  • Later, if we want to build an app(say App B) with the latest version of Flask, we need to upgrade Flask.

  • Now that we have upgraded Flask on our system, we no longer will be able to run the App A smoothly, due to the differences in the versions.

  • Virtual environments come to the rescue. If we create the two apps in 2 different virtual environments and install the corresponding packages or libraries in each virtual environment, both the apps run smoothly.

  • For the above-mentioned reasons, it is a best practice to create any project in a virtual environment. We can create any number of virtual environments which is an incredible thing.

How to create and use a Virtual Environment?

  • Creating a Virtual Environment:

    • We create a virtual environment with the name we mention using the following command: python3 -m venv <<your virtual environment name>>.

    Ex: python3 -m venv my_first_venv creates a virtual environment named my_first_venv.

  • Activating a Virtual Environment:

    • After creating the virtual environment, we switch to that environment to create our project and install the dependencies inside that environment.

    • We switch to a virtual environment by using: source <<your virtual environment name>>/bin/activate

    Ex: source my_first_venv/bin/activate command switches to the virtual environment named my_first_venv.

  • Deactivating a Virtual Environment:

    • To exit the environment, we just need to use the command: deactivate

The following commands should be executed in the console.

  • If you haven't yet switched to Image-Classification-App directory yet, perform

    cd ~
    cd Image-Classification-App
  • Let us create a virtual environment for the Flask App we are going to build now.

    Create a virtual environment named Img-Class-Env using the command virtualenv:

    python3 -m venv Img-Class-Env
  • Activate the virtual environment Img-Class-Env:

    source Img-Class-Env/bin/activate
Get Hint See Answer

Note - Having trouble with the assessment engine? Follow the steps listed here

Loading comments...