Skip to main content
Flask Basics Tutorial
CHAPTER 02 Beginner

Setting Up Python and Flask Environment

Updated: May 14, 2026
20 min read

# CHAPTER 2

Setting Up Python and Flask Environment

1. Introduction

To build applications with Flask, your computer must understand the Python programming language. Furthermore, professional Python development requires the use of Virtual Environments to keep project dependencies organized. In this chapter, we will install Python, configure an isolated workspace, and use the pip package manager to install the Flask framework.

2. Learning Objectives

By the end of this chapter, you will be able to:
  • Install Python and verify it using the command line.
  • Understand the importance of Virtual Environments.
  • Create and activate a Python venv.
  • Install Flask using pip.
  • Generate a requirements.txt file.

3. Beginner-Friendly Explanation

Imagine you are an artist with a giant toolbox (Your Computer). If you throw all your oil paints, watercolors, and charcoals into one massive pile, they will mix together and ruin each other. When you start a new painting, you should get a small, clean tray (A Virtual Environment) and only put the exact colors you need for that specific painting onto the tray. A Virtual Environment is a tiny, isolated copy of Python. When you install Flask inside it, it stays inside that tray. It does not affect any other Python projects on your computer.

4. Step 1: Installing Python

  1. 1. Visit the official Python website: python.org/downloads.
  1. 2. Download the latest stable version for your Operating System.
  1. 3. CRITICAL FOR WINDOWS USERS: During the installation wizard, you MUST check the box that says "Add Python to PATH" before clicking Install. If you miss this, your terminal will not recognize Python commands.

Verify Installation: Open your terminal (Command Prompt/PowerShell for Windows, Terminal for Mac/Linux) and type:

bash
1
python --version

*(Mac/Linux users may need to type python3 --version).*

5. Step 2: Creating a Virtual Environment

Navigate to the location on your computer where you want to store your code.
bash
123456
# Create a new directory for your project
mkdir my_flask_app
cd my_flask_app

# Create the virtual environment (We will name it 'venv')
python -m venv venv

*(Mac/Linux users: Use python3 -m venv venv)*

This creates a new folder named venv inside your project directory containing your isolated Python workspace.

6. Step 3: Activating the Virtual Environment

You must "turn on" the environment before you install anything.

For Windows:

bash
1
venv\Scripts\activate

For Mac/Linux:

bash
1
source venv/bin/activate

*Success Indicator:* Your terminal prompt will change. It will now have (venv) written at the very beginning of the line!

7. Step 4: Installing Flask

Now that we are safely inside our isolated environment, we will use pip (Python's official package installer) to download Flask.
bash
12
# Ensure your virtual environment is active first!
pip install Flask

To verify the installation, type:

bash
1
flask --version

*You should see output detailing the Flask version and the Werkzeug version.*

8. Backend Workflow: Generating requirements.txt

When you share your code with a friend or deploy it to a live server, you do NOT upload the venv folder. Instead, you create a list of instructions telling the other computer what to install.

Run this command:

bash
1
pip freeze > requirements.txt

This generates a text file containing the exact version of Flask you are using. Another developer can simply run pip install -r requirements.txt to instantly replicate your environment!

9. Best Practices

  • The .gitignore File: If you use Git, always create a .gitignore file and add venv/ to it. The virtual environment contains thousands of system-specific files that should never be pushed to a public repository like GitHub.

10. Common Mistakes

  • Installing Globally: Beginners often forget to activate their virtual environment (venv\Scripts\activate) before running pip install Flask. This installs Flask on the global system. Six months later, if they need an older version of Flask for a legacy project, the global installation causes massive version conflicts. Always check for the (venv) tag in your terminal.

11. Exercises

  1. 1. What is the purpose of the pip command in Python? How is it related to the requirements.txt file?

12. Coding Challenges

  • Challenge: Open your terminal. Deactivate your current environment by typing deactivate. Create a brand new folder called flask_practice. Inside it, create a new virtual environment called env. Activate it, and install Flask.

13. MCQs with Answers

Question 1

What is the primary purpose of a Python Virtual Environment (venv)?

Question 2

Which command is used to export a list of all currently installed Python packages into a text file for deployment?

14. Interview Questions

  • Q: Explain what happens if you forget to check the "Add Python to PATH" box during a Windows installation. How does the Operating System locate executable commands?
  • Q: Why is it considered a severe anti-pattern to push your venv/ directory to a GitHub repository? What is the correct workflow for sharing dependencies?

15. FAQs

Q: I get an error saying execution of scripts is disabled on this system when I try to activate on Windows PowerShell. A: Windows restricts script execution by default. Open PowerShell as an Administrator and run: Set-ExecutionPolicy Unrestricted -Scope CurrentUser. Then try activating again.

16. Summary

In Chapter 2, we transformed our computer into a professional Python development machine. We installed the Python runtime, learned the critical importance of isolating our dependencies using Virtual Environments (venv), and successfully activated our workspace. Finally, we utilized Python's package manager (pip) to securely install the Flask framework and generated a requirements.txt file to document our dependencies.

17. Next Chapter Recommendation

Our environment is ready. Before we write code, we must understand how a web server actually thinks. Proceed to Chapter 3: Understanding Flask Architecture.

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·