3. Create a Data Science Project with uv
Duration1h30 AI Banned
Introduction
You will now create the Sales project from an empty directory. Rather than copying a prepared environment, you will ask uv to define the project, install Python, declare its dependencies, create an isolated environment, and lock the selected package versions. This is the same workflow you will use to reproduce and maintain later data science projects.
Create the project directory
Open the workspace created in the previous activity and run:
|
|
The --bare option creates only the minimal pyproject.toml required by uv. You will add the structure that is relevant to a notebook project yourself.
Create the project directories and its initial documentation:
|
|
Your project should now look like this:
|
|
Checkpoint 1
Run pwd and ls -la. You must be inside sales-analysis, and pyproject.toml must be visible before you install Python or any library.
Select the Python version
Install and pin Python 3.12 for this project:
|
|
The pin command creates .python-version. This small text file tells uv and other tools which Python version the project expects.
Check the selected version:
|
|
The first uv run also creates the project environment in .venv if it does not already exist.
Add the project dependencies
Add the libraries required by the Sales analysis:
|
|
Add ipykernel as a development dependency so that VS Code can execute the notebook with this environment:
|
|
Dependencies and development dependencies
The analysis libraries are direct project dependencies because the notebook needs them to run. ipykernel is a development tool used to connect the project environment to an interactive notebook, so uv records it in the dev dependency group. A dependency group is not an optional extra: do not use --extra dev.
Open pyproject.toml in VS Code and identify:
- the project name and version;
- the required Python version;
- the main
dependencieslist; - the
devdependency group.
Understand the generated environment
After adding the dependencies, the project contains four important uv elements:
| Element | Role | Tracked by Git later? |
|---|---|---|
pyproject.toml |
Declares the project and its direct dependencies | Yes |
.python-version |
Records the expected Python version | Yes |
uv.lock |
Records the exact resolved dependency versions | Yes |
.venv/ |
Contains the local Python environment and installed packages | No |
uv add updates pyproject.toml, resolves the dependencies, updates uv.lock, and synchronizes .venv. You can explicitly request the same synchronization later with:
|
|
The --locked option checks that uv.lock agrees with pyproject.toml instead of silently changing the lockfile.
Validate the project
Run Python through uv and inspect the active executable:
|
|
The executable path must point to .venv/bin/python inside sales-analysis.
Checkpoint 2
The project contains pyproject.toml, .python-version, uv.lock, and .venv; pandas imports successfully; and sys.executable points to the project .venv.
Initialize the README
Open README.md in VS Code and add the following structure. You will complete it as your analysis progresses.
|
|
Activity complete
Your Sales project has been built from an empty directory and can run Python through uv. In the next activity, you will connect this environment to a notebook in VS Code.