LESSON 4

 

Creating your development environment

Lesson 4

Screen 1 of 16

Course progress review

This is lesson 4 of 8

 

1) Components of a Web application

 2) Web frameworks and Flask 

3) Installing Python and packages

4) Creating your development environment

5) Building your first local Flask app

6) Running your first Flask app

7) Web application deployment

8) Deploying to PythonAnywhere

Lesson 4

Screen 2 of 16

Lesson 4 outcomes

This lesson prepares you to:

 

  • Create a new development environment for your Flask app by: 
    • setting up directories and files for Flask app
    • installing the virtualenv and Flask packages
    • creating a new virtual environment
    • activating of the virtual environment
    • testing and confirming activation
  • Understand what you have created and why

Lesson 4

Screen 3 of 16

A development environment is virtual space on your local computer which contains the software tools and technologies you use to build your application.

You have installed Python to create your first Flask app.  You will also need to install packages (like Flask) and a place to store your application's files, as you create them.  

Your development environment can hold your files, packages and other tools.  This will help organise your first Flask app project and keep it separated from other projects, as well as your local system files.

Lesson 4

Screen 4 of 16

What is a development environment?

There is no correct way to arrange a Flask Web application development environment.

Most applications/ projects are contained in a top level directory (i.e. the project's 'root directory').

Lesson 4

Screen 5 of 16

Structuring local files and directories                     1/2

c:\~\myproject\

For clarity, we will refer to your new root directory as having the path shown below:

  • Choose a location on your computer for your project's root directory
  • Create a new root directory called 'myproject'

Your new root directory contains your development environment.  You will install packages and other tools into your development environment during this lesson.

Lesson 4

Screen 6 of 16

Structuring local files and directories                     2/2

c:\~\myproject\app
  • Make a new directory called 'app', inside your root directory i.e.

First, you need a directory for your Flask application files.

Next, you need a special directory called a 'virtual environment' inside your root directory.

A virtual environment is an isolated working copy of Python which allows you to work on a specific project without affecting other projects

Lesson 4

Screen 7 of 16

What is a virtual environment?

Advantages

of a virtual environment

  • enable multiple, side-by-side installations of Python (one for each project)
  • allow a single, system-wide copy installation of Python to behave as a discrete installation of Python, in each virtual environment
  • keep different project environments separated

Lesson 4

Screen 8 of 16

Virtual environments:

Create a virtual environment in 3 steps

Step 1. Installation

You will install the virtualenv package into your project's root directory using the pip package manager

Lesson 4

Screen 9 or 16

Step 3. Activation

You will activate your new VE and verify its activation

Step 2. Creation

You will use the virtualenv package to create a new virtual environment (VE) inside your project's root directory

next we will guide you through each step

Creating a virtual environment - STEP 1

 

To install the virtualenv package with pip:

1. Open a command prompt (cmd)

  • type cmd into windows search and press return

  • select the cmd launcher icon to open cmd

Lesson 4

Screen 10 of 16

next we will create the virtual environment

pip install virtualenv

2. Type the following into cmd, then press return

  • type cmd into windows search and press return
  • select the cmd launcher icon to open cmd

Creating a virtual environment - STEP 2

To create a virtual environment:

  • Change into your project's root directory with cmd, using the cd command, and then pressing return:

 

  • Create a new virtual environment, inside your project's root directory, by typing the following into cmd, then pressing return:

Lesson 4

Screen 11 of 16

next we will activate the virtual environment

cd \~\myproject\
virtualenv venv
cd venv

The previous command created a new directory called venv, and installed some default packages and modules (called 'dependancies'), required by your new virtual environment.

  • Change ​into (cd) the new venv directory by typing the following into cmd, then press return.

Creating a virtual environment - STEP 3

To activate your virtual environment:

Lesson 4

Screen 12 of 16

cd \~\myproject\venv\Scripts

2. Run the 'activate.bat' script to activate your virtual environment, by typing the following command and pressing return.

1. Change into the Scripts directory, inside your venv directory (as shown), with cmd and the cd command. Press return.

activate.bat

3. The virtual environment is active if your command line begins with (venv), in cmd (as shown).

(venv) ~\myproject\venv\Scripts

To deactivate an active virtual environment: select the cmd window you started the virtual environment from and press the keys ctrl + c.

Repeat the previous steps to reactivate it.

Install the Flask package with pip

Lesson 4

Screen 13 of 16

Your development environment is ready for use.

You can use the pip package manager to install packages into an active virtual environment.  The packages you install inside a virtual environment will not be installed system-wide.  This retains the current state of your computer's Python installation.

(venv) pip install flask

To install Flask into your virtual environment:

  • activate your virtual environment
  • type the following command into cmd and press return

Virtual environments quiz

Lesson 4

Screen 14 of 16

Complete the quiz to test your knowledge of virtual environments.

Lesson 4 review

You have completed Lesson 4 on creating your development environment.  Now you can:

 

  • understand the concept of a virtual environment
  • install a virtual environment in your project's root directory
  • install packages for Python
  • activate and deactivate a virtual environment
  • verify that activation has taken place 

Lesson 4

Screen 15 of 16

Next lesson

Proceed to Lesson 5, where you will use your development environment to create your first local Flask Web application.

Lesson 4

Screen 16 of 16

Made with Slides.com