A virtual environment is an isolated workspace that allows you to manage project-specific dependencies without affecting other projects or the system-wide Python installation.
Dependency Management: Different projects may require different versions of the same packages.
Avoid Conflicts: Isolating environments prevents package conflicts between projects.
Reproducibility: Ensures that your project runs the same way on different machines.
Accept the GitHub Classroom invite
Open your Web Programming folder using VS Code
Clone the GitHub repository for this assignment.
E.g. git clone https://github.com/mullumhs/project-18-flask-your_username.git
Ensure Python is installed on your machine
cd into the project-18-flask folder in the VS Code terminal
Create a virtual environment by typing: python3 -m venv venv OR python -m venv venv
(This command uses Python's built-in venv module to create a virtual environment.)
Activate the environment to start using it:
Windows: venv\Scripts\activate
macOS/Linux: source venv/bin/activate
You'll notice your terminal prompt now starts with (venv), indicating the virtual environment is active.
With the virtual environment active, install Flask using pip: pip install flask
This installs Flask and its dependencies in your virtual environment.
Steps so far:
Create a file using touch in the terminal named app.py and add the following code:
from flask import Flask
app = Flask(__name__)
if __name__ == '__main__':
app.run(debug=True)
from flask import Flask: Imports the Flask class from the flask module.
app = Flask(__name__): Creates a new Flask application instance.
if __name__ == '__main__':: Ensures the app runs only if the script is executed directly.
app.run(debug=True): Starts the development server with debugging enabled.
If your installation of VS Code has the Python extension installed, click the run button.
Otherwise, type into your terminal: python3 app.py OR python app.py
You should see something like this:
Holding down Ctrl and clicking on http://127.0.0.1:5000, you should see:
This is because we haven't added any routes or pages to our site yet. This is the default 404 (page not found) error message!
But, it does mean our server is working!
Remember to commit and push your code when done! Here are the terminal commands to do so:
Commit your changes with a meaningful message:
git commit -am "Your message here"
Note: The -a flag automatically stages all modified files before committing, -m is to add a message.
Push your changes to GitHub:
git push origin main