- Lab
-
Libraries: If you want this lab, consider one of these libraries.
- Core Tech
Guided: First uv Project
In this Code Lab, you will learn to use uv, a fast Python package installer, to set up a project in an offline environment. You'll initialize a virtual environment, install dependencies from local files, manage dependency groups, generate requirements files, verify the setup, and build a comprehensive Python application.
Lab Info
Table of Contents
-
Challenge
Introduction to `uv`
Welcome to this code lab on
uv!uvis a modern, extremely fast Python package installer and resolver written in Rust. It's designed as a drop-in replacement for common tools likepip,pip-tools, andvenv.In this lab, you'll simulate a common real-world scenario: setting up a project in an environment without internet access. You'll see how
uvfeatures make this process straightforward and efficient.You'll learn to:
- Create and manage virtual environments
- Install dependencies from local sources
- Work with optional dependency groups
- Generate requirements files and lock files
- Verify dependency integrity
- Build a real-world application
Begin by exploring the project structure. You have a
main.pyfile for your application code, apyproject.tomlfile defining dependencies, and awheelsdirectory containing pre-downloaded packages. info > This lab experience was developed by the Pluralsight team using Forge, an internally developed AI tool utilizing Gemini technology. All sections were verified by human experts for accuracy prior to publication. For issue reporting, please contact us. -
Challenge
Initializing the Project Environment
uvcan function as a complete toolchain for managing your Python projects. The first step in any project is to create an isolated virtual environment to manage its dependencies separately from other projects on your system.uvsimplifies this with theuv venvcommand, which is significantly faster than Python's built-inpython -m venv. -
Challenge
Installing Dependencies Offline
Managing dependencies is a core task in any project. The
pyproject.tomlfile is the modern standard for defining project metadata and dependencies. Take a look at this project'spyproject.tomlto see the packages you need:requestsandrichfor the core application.Because the environment is offline, you can't fetch these from PyPI.
uvhandles this gracefully with the--find-linksflag, which tells it to look for packages in a specified local directory. You'll combine this with--no-indexto ensureuvdoesn't attempt any network requests. Many projects organize dependencies into groups. Optional dependencies let you install only what you need for specific use cases. For example, development dependencies like testing frameworks are typically separated from production dependencies.In this project, the
pyproject.tomldefines adevoptional dependency group containingpytestfor testing. You'll install these optional dependencies next. -
Challenge
Managing Dependencies and Ensuring Reproducibility
After installing dependencies, it's good practice to generate requirements files and lock files for reproducibility.
uvprovides several commands for this:uv pip compile– Resolves dependencies frompyproject.tomland generates arequirements.txtfile with pinned versions.uv pip freeze– Outputs currently installed packages in a format suitable for a requirements file.uv pip check– Validates that all installed packages are compatible with one another.
These tools help ensure that your project can be reliably reproduced across different environments.
-
Challenge
Developing and Running the Application
Now for the final step: putting your installed dependencies to use. You will complete a Python script that processes data and displays it in a formatted table. This confirms that the environment is set up correctly and the packages are fully functional.
Your script will read from a local
data.jsonfile, which contains structured data about items. You'll use therequestslibrary to fetch this data (even for local files) and therichlibrary to create formatted tables and console output.This is a more complex application than a simple example. You'll process multiple data items, aggregate information, and present it in a professional format. Testing is an essential part of software development. Now that you have
pytestinstalled as a development dependency, you'll use it to verify that your application works correctly. The project includes test files that validate your implementation. -
Challenge
Conclusion
Congratulations! You have successfully set up a Python project using
uvin a simulated offline environment.You've learned how to:
- Create a virtual environment with
uv venv. - Install core dependencies from a local source using
uv pip sync. - Install optional dependency groups using
--extra. - Generate requirements files with
uv pip compile. - Create lock files with
uv pip freeze. - Verify dependency integrity with
uv pip check. - Run tests with pytest.
- Build a comprehensive Python application.
These are the foundational skills you need to adopt
uvin your own projects for faster, more reliable dependency management. The offline workflow you've practiced is essential for production environments, air-gapped systems, and CI/CD pipelines where network access may be restricted. - Create a virtual environment with
About the author
Real skill practice before real-world application
Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.
Learn by doing
Engage hands-on with the tools and technologies you’re learning. You pick the skill, we provide the credentials and environment.
Follow your guide
All labs have detailed instructions and objectives, guiding you through the learning process and ensuring you understand every step.
Turn time into mastery
On average, you retain 75% more of your learning if you take time to practice. Hands-on labs set you up for success to make those skills stick.