Flask / Flask Installation and Setup

Setting Up and Activating Virtual Environments

In this tutorial, you'll learn how to set up and activate a virtual environment for your Flask projects. Virtual environments allow you to manage dependencies for different projec…

Tutorial 2 of 5 5 resources in this section

Section overview

5 resources

Explains how to install and set up Flask on different platforms and environments.

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!

Need Help Implementing This?

We build custom systems, plugins, and scalable infrastructure.

Discuss Your Project

Related topics

Keep learning with adjacent tracks.

View category

HTML

Learn the fundamental building blocks of the web using HTML.

Explore

CSS

Master CSS to style and format web pages effectively.

Explore

JavaScript

Learn JavaScript to add interactivity and dynamic behavior to web pages.

Explore

Python

Explore Python for web development, data analysis, and automation.

Explore

SQL

Learn SQL to manage and query relational databases.

Explore

PHP

Master PHP to build dynamic and secure web applications.

Explore

Popular tools

Helpful utilities for quick tasks.

Browse tools

Case Converter

Convert text to uppercase, lowercase, sentence case, or title case.

Use tool

WHOIS Lookup Tool

Get domain and IP details with WHOIS lookup.

Use tool

Scientific Calculator

Perform advanced math operations.

Use tool

EXIF Data Viewer/Remover

View and remove metadata from image files.

Use tool

URL Encoder/Decoder

Encode or decode URLs easily for web applications.

Use tool

Latest articles

Fresh insights from the CodiWiki team.

Visit blog

AI in Drug Discovery: Accelerating Medical Breakthroughs

In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…

Read article

AI in Retail: Personalized Shopping and Inventory Management

In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …

Read article

AI in Public Safety: Predictive Policing and Crime Prevention

In the realm of public safety, the integration of Artificial Intelligence (AI) stands as a beacon of innovati…

Read article

AI in Mental Health: Assisting with Therapy and Diagnostics

In the realm of mental health, the integration of Artificial Intelligence (AI) stands as a beacon of hope and…

Read article

AI in Legal Compliance: Ensuring Regulatory Adherence

In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…

Read article

Need help implementing this?

Get senior engineering support to ship it cleanly and on time.

Get Implementation Help