Setting Up Python and Flask Environment
# 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 thepip 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.txtfile.
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. Visit the official Python website: python.org/downloads.
- 2. Download the latest stable version for your Operating System.
- 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:
*(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.*(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:
For Mac/Linux:
*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.To verify the installation, type:
*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 thevenv folder. Instead, you create a list of instructions telling the other computer what to install.
Run this command:
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
.gitignoreFile: If you use Git, always create a.gitignorefile and addvenv/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 runningpip 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.
What is the purpose of the
pipcommand in Python? How is it related to therequirements.txtfile?
12. Coding Challenges
-
Challenge: Open your terminal. Deactivate your current environment by typing
deactivate. Create a brand new folder calledflask_practice. Inside it, create a new virtual environment calledenv. Activate it, and install Flask.
13. MCQs with Answers
What is the primary purpose of a Python Virtual Environment (venv)?
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 sayingexecution 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.