Environment Setup
Duration30minIntroduction
Setting up a proper development environment is crucial for data science work. This tutorial guides you through installing uv, the modern Python package manager, and understanding modern Python project standards with pyproject.toml configuration.
A well-configured environment ensures:
- Reproducibility: Your code works consistently across different machines
- Speed: Fast dependency installation and resolution
- Simplicity: No manual virtual environment management
- Modern Standards: Following current Python development practices
Prerequisites
- Basic command line knowledge
- Administrator/sudo access on your machine
- Internet connection for package downloads
Development Environment Setup
Visual Studio Code Installation
Visual Studio Code (VS Code) is the recommended IDE for this course. It provides excellent Python support, integrated terminal, and extensions for data science.
Installation:
-
Download VS Code:
- Visit https://code.visualstudio.com/
- Download for your operating system
- Install with default settings
-
Essential Extensions:
- Python: Official Python extension by Microsoft (provides IntelliSense, debugging, and code completion)
- Pylance: Advanced Python language server (enhanced error detection and type checking)
- Jupyter: Jupyter notebook support (edit and run notebooks directly in VS Code)
- GitLens: Enhanced Git capabilities (version control integration within the editor)
- Remote - WSL: For Windows users (enables seamless WSL integration)
-
Python Extension Setup:
- Open VS Code
- Install Python extension
- Access integrated terminal (
Ctrl+``orView → Terminal) to run Python scripts directly
Platform-Specific Setup
Windows Users: WSL2 Setup
Why WSL2? Windows Subsystem for Linux provides a native Linux environment that ensures compatibility with most data science tools and matches deployment environments.
-
Enable WSL2:
1 2# Run in PowerShell as Administrator wsl --install -
Install Ubuntu:
1wsl --install -d Ubuntu -
Set up Ubuntu:
- Launch Ubuntu from Start menu
- Create username and password
- Update packages:
sudo apt update && sudo apt upgrade
-
Access from VS Code (recommended):
- Install “Remote - WSL” extension
- Open folder in WSL:
code .from Ubuntu terminal
macOS and Linux Users
Your system is ready for Python development. Proceed to uv installation.
Modern Python Environment with uv
Why uv? uv is a modern Python package manager that combines the functionality of pip, virtualenv, and pyenv into a single, fast tool. It’s written in Rust and provides significant performance improvements over traditional tools. UV provide the following features:
- Automatic Python Installation: uv can install Python versions automatically
- Built-in Virtual Environments: No need to manually create or activate environments
- Fast Dependency Resolution: 10-100x faster than pip
- pyproject.toml Native: Works seamlessly with modern Python project standards
- Lock Files: Automatically creates uv.lock for exact reproducibility
Reference:
Install uv
All platforms:
|
|
Verify installation:
|
|
Si cela ne fonctionne pas il faut fermer et relancer votre path ou recharger votre shell en faisant par exemple source ~/.zshrc si vous utilisez zsh.
Working with uv
Creating Your First Project
|
|
Python Version Management
|
|
Managing Dependencies
Proper dependency management is crucial for reproducible and maintainable data science projects. It ensures that your code works consistently across different environments and team members can easily set up the same development environment.
Understanding uv’s Dependency Files
When working with uv, two key files manage your project dependencies:
pyproject.toml: Defines your project requirements and version constraintsuv.lock: Contains exact pinned versions for reproducible installations
Adding Dependencies
|
|
This creates a pyproject.toml file with your project configuration:
|
|
Version Constraints:
~=2.3.1: Compatible release (allows2.3.xbut not2.4.0)>=3.11: Minimum version required
Locking Dependencies
To ensure reproducible installations, lock your dependencies to exact versions:
|
|
This generates uv.lock with pinned versions:
|
|
Installing the Virtual Environment
When joining a project or setting up on a new machine you can just import pyproject.toml and uv.lock files.
After importing these files or adding dependencies manually run the installation in an isolated virtual environment.
|
|
This command includes both core dependencies and dev group.
Examining the Environment
View your installed packages and their dependency tree:
|
|
Example output:
sales_analysis v1.0.0
├── pandas v2.3.1
│ ├── numpy v2.3.1
│ ├── python-dateutil v2.9.0
│ └── pytz v2024.2
├── matplotlib v3.10.3
│ └── numpy v2.3.1
└── seaborn v0.13.2
├── matplotlib v3.10.3
└── pandas v2.3.1Optional Dependencies
Organize dependencies into groups for different use cases:
|
|
This approach allows team members to install only the dependencies they need for their specific tasks.
Running Python Code
|
|
VSCode and Jupyter Notebooks with uv
Common Jupyter Kernel Issue
When working with Jupyter notebooks in VSCode using uv environments, you might encounter the “ipykernel not installed” error even though ipykernel is present in your pyproject.toml and correctly installed in the virtual environment.
Why does this happen?
- VSCode looks for Jupyter kernels in system-wide locations
- uv kernels are installed locally in the project’s
.venv/directory - By default, VSCode cannot see these local kernels
Register the Kernel for VSCode
To make VSCode see and use your uv environment with Jupyter, register the kernel at the user level:
|
|
Command parameters:
--user: Install for current user (no sudo required)--name: Technical kernel name (no spaces)--display-name: Display name in VSCode (can contain spaces)
Alternatively, you can in VSCode press CRTL+Shift+P and search Python: Select Interpreter and either choose the interpreter or specify the path to it. To find that path : uv run which python.
Configure Notebook kernel in VSCode
-
Restart VSCode completely to detect new kernels
-
Open your notebook (
.ipynbfile) -
Select the kernel:
- Click the kernel selector in the top-right of the notebook
- Or use
Ctrl+Shift+P→ “Notebook: Select Notebook Kernel”
-
Choose your uv kernel:
- Look for “Air Quality (uv)” in the list
- Select the kernel with the name you specified
Modern Python Project Standards
Understanding pyproject.toml
Modern Python projects use pyproject.toml for configuration. This follows PEP 518 and PEP 621 standards and is automatically created by uv.
Example pyproject.toml structure:
|
|
Working with Optional Dependencies
|
|
Creating pyproject.toml manually
If you need to create a pyproject.toml file manually, here’s a template:
|
|
After creating this file, run:
|
|
Validation
Before proceeding to practical sessions, verify your setup:
|
|
Expected outputs:
- uv version displayed
- Project created successfully
- Dependencies installed quickly
- Python imports work correctly
Summary
You now have:
- ✅ Modern Python environment with uv
- ✅ Automatic virtual environment management
- ✅ Fast dependency installation and resolution
- ✅ Understanding of pyproject.toml configuration
- ✅ Simplified development workflow