Programming is about solving problems, not just writing code. Labs are not a race; they are a marathon, so take your time. Do not copy and run blindly. Think first, and if you get stuck, ask Stelios.
In this first tutorial, you will prepare your workspace. Pay attention, as you will need to follow these steps in future classes.
- Install Git if it is not already installed from here. For Mac:
brew install git. For Windows: download and install it. - Install Visual Studio Code (or another editor you prefer). I will use VS Code in this module. After installing Git, restart VS Code if it was already open.
- Open a terminal.
- Clone the class repository:
git clone https://github.com/warestack/bdaTip
If the repository already exists, run git pull instead of cloning again.
- Open the project folder in VS Code.
- Open a terminal inside VS Code.
- You will need to navigate to folders using the
cdcommand.
Check that Python is installed:
python3 --versionOn Windows, you can also run:
python --version
python3 --version
py --versionDepending on your Python installation on Windows, one of the above commands may work while others may not.
This part is a recap of the basics. We will reuse these steps in future sessions.
-
Python: the programming language, not the snake 🐍. -
Terminal: a text-based tool where you run commands likepython3 --versionorpip install. -
cd: changes directory (moves you into another folder). -
pwd: prints your current folder path. -
Virtual environment (.venv): keeps each project’s Python packages separate, so different projects don’t conflict. Different projects often need different package versions; isolation avoids conflicts. -
pip: Python’s package manager; it installs libraries likehuggingface_hubordatasets. -
requirements.txt: a list of required Python packages for the project. It lets everyone install the same dependencies and reproduce the same setup. -
README.md: a simple project file where you document what you built, what worked, and what is pending (useful for tracking progress). Not sure about Markdown syntax? Check here. -
solutions/: the folder you need to create to store your answers for each tutorial part (no need to submit now, but you can share it with Stelios later, for example for homework). -
session_solutions/: reference solutions provided by Stelios. Use them only to review your work after you attempt the tasks. -
Naming convention for exercise files in
solutions/: useexercise-<session>-<part>.py(for example,exercise-01-02.py). Helper library modules can use underscores (for example,exercise_01_02_lib.py).
You will need to navigate folders in the terminal using cd.
Quick examples (macOS/Linux):
pwd
cd session1
pwd
cd ..Quick examples (Windows PowerShell):
Get-Location
cd session1
Get-Location
cd ..You will need a virtual environment to install the required packages.
Tip
Make sure you are in the correct folder before creating it. You can create one environment per session (recommended) or use one environment for the entire bda project.
Navigate to the folder using cd session1.
Create a virtual environment:
On macOS/Linux:
python3 -m venv .venvOn Windows:
python -m venv .venv
# or
py -m venv .venvActivate the environment:
source .venv/bin/activateOn Windows (VS Code terminal):
- PowerShell:
.venv\Scripts\Activate.ps1(may be blocked by execution policy on some machines)- Optional temporary PowerShell bypass (current session only):
Set-ExecutionPolicy -Scope Process -ExecutionPolicy BypassThen run:.venv\Scripts\Activate.ps1- If activation is blocked, run scripts directly with:
.venv\Scripts\python.exe your_script.py
Deactivate it when needed:
deactivateNow check the requirements.txt file. It contains the dependencies we need:
huggingface_hub
datasets
requests==2.32.3
quizmdTip
A requirements.txt file lists all Python packages a project needs. It helps everyone recreate the same environment. Pin exact versions when reproducibility is critical.
Activate .venv again and install dependencies:
pip install -r requirements.txtIf pip is missing, run:
python3 -m ensurepip --upgrade
python3 -m pip install --upgrade pipCheck the output to ensure everything installed successfully. You can ignore most warnings for the moment.
You are now ready to proceed. You can use the clear command to clear the terminal. Try it out.
For now, minimise the current VS Code window. On your Desktop (or any folder you prefer), create a new folder called test-folder.
- Open a new VS Code window and open the
test-folder. - Create and activate a virtual environment.
- Create a
requirements.txtfile, add therequestslibrary (2.32.3), and install it. - Create a file named
exercise-01-01.pyand run the script below.
File: test-folder/exercise-01-01.py
import re
from urllib.request import urlopen
url = "https://www.google.com"
with urlopen(url) as response:
html = response.read().decode("utf-8", errors="ignore")
match = re.search(r"<title>(.*?)</title>", html, re.IGNORECASE | re.DOTALL)
print(match.group(1).strip() if match else "No title found")This exercise is more about learning how to manage your .venv. When finished, keep exercise-01-01.py as your solution file.
Tip
Why dot in .venv?
The . makes the folder hidden by default, keeping your project directory clean.
quizmd is a command-line interface (CLI) to run interactive quizzes in the lab (made by Stelios 🙂). You can still install it like this.
pip install quizmdRun the quiz in your terminal and ask Stelios for help if you’re unsure. Solutions are in the
quizzesfolder, but don't see them yet.
Start the quiz by selecting a theme that matches your terminal:
Use the default theme (no --theme needed):
quizmd quizzes/python-workspace-setup-quiz.mdOr choose a theme:
Use --theme light if your terminal has a light background or --theme dark if your terminal has a dark background.
quizmd --theme light quizzes/python-workspace-setup-quiz.md
quizmd --theme dark quizzes/python-workspace-setup-quiz.mdFor a focus-mode, try:
quizmd --full-screen --theme dark quizzes/python-workspace-setup-quiz.mdFor accessibility use this: quizmd --no-color quizzes/python-workspace-setup-quiz.md
Important
Review the quiz rules before you start.
Use the space bar to select an option, then press Enter to move to the next question. Go ahead and complete the quiz 🎉!
You are now ready to move to the next tutorial.