PYVERSE LESSON: SETTING UP PYTHON AND FLASK ENVIRONMENT
Goal: By the end of this lesson, you will have Python and Flask installed, a project folder created, and a simple web app running on your computer.
1) WHAT YOU WILL LEARN
- What Python and Flask are (in simple words)
- How to install Python
- How to use a virtual environment (venv)
- How to install Flask
- How to create and run your first Flask web app
- How to fix common setup errors
2) WHAT ARE PYTHON AND FLASK?
- Python: A programming language that is easy to read and great for beginners.
- Flask: A small (but powerful) tool that helps you build websites and web apps using Python.
Think of Python as the language you speak, and Flask as the set of tools to build a simple website using that language.
3) WHAT YOU NEED
- A computer with internet access (Windows, macOS, or Linux)
- A code editor (Visual Studio Code is recommended)
- About 30–45 minutes and your curiosity
4) STEP 1: INSTALL PYTHON
A) Windows
- Go to: https://www.python.org/downloads/
- Download the latest Python 3.x for Windows.
- Important: On the installer's first screen, check the box "Add Python to PATH".
- Click "Install Now" and finish the setup.
B) macOS
- Option 1: Install from https://www.python.org/downloads/ (simple for beginners).
- Option 2 (if you're comfortable): Install Homebrew, then run:
brew install python
C) Linux
Most Linux systems already have Python 3. Check with: python3 --version
D) Chromebook (optional)
- Turn on Linux (Beta) in Settings.
- Open the Linux terminal and run:
sudo apt update sudo apt install -y python3 python3-venv python3-pip
E) Verify Python installed
- Open your Terminal (macOS/Linux) or Command Prompt/PowerShell (Windows).
- Try:
- Windows:
python --versionorpy --version - macOS/Linux:
python3 --version
- Windows:
You should see something like: Python 3.10.x or newer. If not, restart your terminal and try again.
5) STEP 2: INSTALL A CODE EDITOR (RECOMMENDED: VS CODE)
- Download and install Visual Studio Code: https://code.visualstudio.com/
- Open VS Code after installing. We will use it to edit our Python files.
6) STEP 3: CREATE YOUR PROJECT FOLDER
- Choose a simple place for your folder, like Documents.
- Create a folder named:
flask-hello - Avoid spaces or special characters in the folder name.
Example paths:
- Windows:
C:\Users\YourName\Documents\flask-hello - macOS/Linux:
/Users/YourName/Documents/flask-hello
7) STEP 4: CREATE AND ACTIVATE A VIRTUAL ENVIRONMENT
A virtual environment keeps your project's packages separate and tidy.
A) Open your terminal in the project folder
- In VS Code: File > Open Folder > select
flask-hello - Open the integrated terminal (View > Terminal). Make sure it says you're inside the
flask-hellofolder.
B) Create the virtual environment
Windows (use python or py):
python -m venv venvor
py -m venv venvmacOS/Linux:
python3 -m venv venvThis creates a folder named venv inside your project.
C) Activate the virtual environment
Windows (Command Prompt):
venv\Scripts\activateWindows (PowerShell):
.\venv\Scripts\Activate.ps1If you see a security policy error, open PowerShell as Administrator and run:
Set-ExecutionPolicy -Scope CurrentUser RemoteSignedThen try activation again.
macOS/Linux:
source venv/bin/activateYou'll know it worked if you see (venv) at the start of your terminal line.
To deactivate later:
- Type:
deactivate
8) STEP 5: INSTALL FLASK
With your virtual environment active, run:
If pip works:
pip install flaskIf pip doesn't work, try:
python -m pip install flaskor
python3 -m pip install flaskOptional but helpful:
Upgrade pip:
python -m pip install --upgrade pip9) STEP 6: CREATE YOUR FIRST FLASK APP
A) Inside your flask-hello folder, create a file named: app.py
B) Paste this code into app.py:
from flask import Flask
app = Flask(__name__)
@app.route("/")
def home():
return "Hello, PyVerse! Your Flask app is running."
if __name__ == "__main__":
app.run(debug=True)What this code does:
- Creates a Flask app
- Sets up one page at the URL "/"
- Shows a friendly message
- Runs the app in debug mode so it auto-reloads when you save changes
10) STEP 7: RUN THE APP
- Make sure your virtual environment is active (you see
(venv)in your terminal). - Run:
- Windows:
python app.pyorpy app.py - macOS/Linux:
python3 app.py
- Windows:
Expected output (similar to this):
* Serving Flask app 'app'
* Debug mode: on
* Running on http://127.0.0.1:5000
Press CTRL+C to quit- Open your web browser and go to:
http://127.0.0.1:5000- or
http://localhost:5000
You should see:
Hello, PyVerse! Your Flask app is running.To stop the server:
- In the terminal, press
Ctrl + C.
11) STEP 8: MAKE A SMALL CHANGE
Let's make sure auto-reload works.
- Change the return message in
app.pyto:return "Hello, PyVerse! You changed the code and Flask auto-reloaded." - Save the file.
- Look at your browser again and refresh. The new message should appear without restarting the server.
12) PROJECT FOLDER STRUCTURE
You should have something like this:
flask-hello/
venv/ (your virtual environment folder)
app.py (your Flask app code)Note: The venv folder can be large. That's normal.
13) COMMON ERRORS AND QUICK FIXES
- "python is not recognized" (Windows):
- Try:
py --version - If that works, use
pyinstead ofpython. - If not, reinstall Python and check "Add Python to PATH".
- Try:
- "pip is not recognized":
- Use:
python -m pip install flask - or make sure your virtual environment is activated.
- Use:
- "ModuleNotFoundError: No module named 'flask'":
- You forgot to install Flask or your venv isn't active.
- Fix: Activate venv, then run:
pip install flask
- Permission errors on PowerShell:
- Run PowerShell as Administrator and:
Set-ExecutionPolicy -Scope CurrentUser RemoteSigned - Then activate venv again:
.\venv\Scripts\Activate.ps1
- Run PowerShell as Administrator and:
- Port already in use (server won't start):
- Something else is using port 5000.
- Try:
app.run(debug=True, port=5001)
- Nothing shows in browser:
- Make sure the server is running and you're visiting:
http://127.0.0.1:5000 - Refresh the page and check the terminal for errors.
- Make sure the server is running and you're visiting:
14) CLEAN UP
- Stop the server:
Ctrl + C - Deactivate the virtual environment:
deactivate - To remove everything later, delete the
flask-hellofolder.
15) MINI PRACTICE: ADD A SECOND PAGE
A) Add this route under the home() function:
@app.route("/about")
def about():
return "About: This is my first Flask app!"B) Save and go to:
http://127.0.0.1:5000/about
You should see your About message.
CHALLENGE: PERSONALIZE YOUR APP
- Change the homepage message to include your name or a fun greeting.
- Add a new route
"/hello/<name>"that says hello to any name in the URL.
Hint:
@app.route("/hello/<name>")
def hello(name):
return f"Hello, {name}! Welcome to PyVerse."Try:
http://127.0.0.1:5000/hello/Alex
18) WHAT'S NEXT
- Learn how to return real HTML pages using Flask templates (Jinja).
- Add CSS to make your pages look nice.
- Build a small "To-Do" web app with forms.
APPENDIX: FULL TESTED CODE (app.py)
from flask import Flask
app = Flask(__name__)
@app.route("/")
def home():
return "Hello, PyVerse! Your Flask app is running."
@app.route("/about")
def about():
return "About: This is my first Flask app!"
@app.route("/hello/<name>")
def hello(name):
return f"Hello, {name}! Welcome to PyVerse."
if __name__ == "__main__":
app.run(debug=True)This lesson and code were tested with:
- Python 3.10+ and 3.11+
- Flask 3.0+
Great job! You just set up a real Python web environment and ran your first Flask app.