Setting Up and Activating Virtual Environments

Tutorial 2 of 5

1. Introduction

In this tutorial, we aim to guide you through setting up and activating a virtual environment for your Flask projects. You will learn how to create a virtual environment, install Flask into it, and how to activate and deactivate this environment. By the end of this tutorial, you will have a basic understanding of managing project-specific environments and dependencies.

Prerequisites:
- Basic knowledge of Python programming
- Flask installed on your system
- Access to a terminal/command line interface

2. Step-by-Step Guide

2.1. What is a Virtual Environment?

A virtual environment is a tool that helps to keep dependencies required by different projects separate. It solves the “Project X depends on version 1.x, but Project Y needs 4.x” dilemma. It creates an environment that has its own installation directories and doesn’t share libraries with other virtual environments.

2.2. How to Set Up a Virtual Environment

  • Navigate to the directory where you want to create your new virtual environment.
  • Run python3 -m venv env (Replace "env" with your environment name)

2.3. How to Activate a Virtual Environment

  • On macOS and Linux: source env/bin/activate
  • On Windows: .\\env\\Scripts\\activate

2.4. How to Deactivate a Virtual Environment

  • Regardless of your OS, you can deactivate a virtual environment by typing deactivate in your shell.

3. Code Examples

3.1. Setting Up a Virtual Environment

$ cd my_project_folder # navigate to project folder
$ python3 -m venv env  # create virtual environment

3.2. Activating the Virtual Environment

On macOS and Linux:

$ source env/bin/activate

On Windows:

$ .\\env\\Scripts\\activate

3.3. Deactivating the Virtual Environment

$ deactivate

4. Summary

In this tutorial, we've learned the importance of virtual environments for managing separate dependencies across projects. We've also learned how to set up, activate, and deactivate these environments.

Next steps for learning could include exploring how to manage package dependencies within a virtual environment, and how to use Flask to create basic web applications.

For further reading, consult the official Python documentation on venv.

5. Practice Exercises

  1. Create a virtual environment in a new directory, activate it, and then deactivate it.
  2. Create two separate virtual environments in separate directories. In each one, install different versions of the same package (such as Flask). Check the version of the package in each environment to confirm they are different.

Solutions:

  1. Commands:
  2. mkdir new_project && cd new_project
  3. python3 -m venv new_env
  4. source new_env/bin/activate
  5. deactivate

  6. Commands:

  7. mkdir project_1 && cd project_1
  8. python3 -m venv env1
  9. source env1/bin/activate
  10. pip install Flask==1.0.0
  11. python -c "import flask; print(flask.__version__)" (should output 1.0.0)
  12. deactivate
  13. cd .. && mkdir project_2 && cd project_2
  14. python3 -m venv env2
  15. source env2/bin/activate
  16. pip install Flask==1.1.0
  17. python -c "import flask; print(flask.__version__)" (should output 1.1.0)

Remember to continue practicing with virtual environments to solidify your understanding!