SchoolWhool
Quizzes and saved progress, freeSign in freeSign in

Python 5.2Free video12 minLesson 2 of 7 in Module 5

uv, Packages and Environments

Install Python and packages for a project without breaking other projects.

Free sample. Everything in this module opens with a free account.

Watch or read the lesson: uv, Packages and Environments

01Local development, and conflicting package versions

In the previous lesson, you created your first Python module using Colab's file panel. The next step is to move everything onto your own computer.

But why? You have used Colab for four modules, and it has worked perfectly well. The difference comes down to what you are building. So far, you have mostly worked with individual notebooks. Now you are starting to build an actual Python projectKey termA folder of code with a pyproject.toml that describes it., and a project needs a place where all its files can stay together.

Think about the review analysis project. You have the module you created in the last lesson. You will also have files containing reviews. And later in this module, you will write tests to make sure your code works correctly. All of those files need to live somewhere permanent.

Colab does let you create files, but files stored only in its temporary session storage can disappear when that session ends. That is fine when you are experimenting in a notebook. It is not ideal when you are building something you want to keep working on.

There is another reason to move. You are going to start using tools like debuggers, test runners and code checkers, and these tools work well when your code is organized into a proper project folder. So from this lesson onward, you will work with a project on your own computer. You can still use Colab whenever you want.

Two projects, two versions

There is another problem to solve. Remember how you used NumPy in Colab? You wrote import numpy, and it worked. You did not have to install anything, because Colab already had NumPy available. On your own computer, you will need to manage the packages your projects depend on. A package a project needs in order to run is called a dependencyKey termA package a project needs, listed in pyproject.toml..

That gets interesting when you have more than one project. Say your first project needs NumPy version 2.5, but you also have an older project that was built to work with NumPy version 2.3. Now imagine both projects use the same Python environment. If you install NumPy 2.5 for the first project, you might replace the version the older project was using, and suddenly the older project might stop working.

So how do you make sure each project gets exactly the packages it needs, without affecting any other project? That is what this lesson solves, and the tool you will use is called uv.

02Installing uv and a Python interpreter

Before getting into uv, there is one new tool to introduce: the terminalKey termA window where you type commands for the computer, one line at a time: Terminal on macOS, PowerShell on Windows.. This is the first lesson in the course that uses it. If you have never used one before, it is straightforward. A terminal is simply a place where you type commands and ask your computer to do things. You type a command, press Enter, and the computer runs it. On macOS, you can use the Terminal app. On Windows, you can use PowerShell. Most of the commands in this lesson start with uv.

Installing uv

uv is a tool that helps you manage Python, create projects and install packages. Instead of doing all of those things separately, you can handle them with one tool.

First, you need to install uv itself. Its official documentation lists the installation commands: one for macOS and Linux, and another for Windows. Use the command for your operating system. On macOS and Linux, it is:

curl -LsSf https://astral.sh/uv/install.sh | sh

On Windows, in PowerShell, it is:

powershell -ExecutionPolicy ByPass -c "irm https://astral.sh/uv/install.ps1 | iex"

Once the installation finishes, open a new terminal and type:

uv --version

This prints the version of uv installed on your computer, a line that starts with uv followed by a version number. Your version may well be newer than the one used to check this walkthrough, and that is completely fine.

Installing Python

Now that you have uv, install Python with it:

uv python install 3.12

This tells uv to install Python 3.12. You are choosing Python 3.12 so that you work with a consistent version throughout this part of the course. The program that reads and runs your Python code is called the Python interpreter, and this command installs one.

Here is something useful: uv keeps the Python versions it manages in its own location. So installing Python this way does not replace an existing Python installation on your computer.

03Creating a project with uv init

Now you are ready to create your first project. In the terminal, go to the folder where you want to keep your projects, and type:

uv init --package review-stats --python 3.12

This creates a new project called review-stats, using a package-style project structure. uv replies with one line saying it initialized the project review-stats, followed by the folder's full path on your machine.

What uv created

Open that folder and look at what you have:

review-stats/
├── .gitignore
├── .python-version
├── README.md
├── pyproject.toml
└── src/
    └── review_stats/
        └── __init__.py

First, there is a file called pyproject.toml. This is one of the most important files in a modern Python project: it describes the project and its dependencies. There is also a .python-version file, which tells uv which Python version you want to use:

3.12

There is a README file. And there is a folder called src. Inside src is another folder called review_stats, with an __init__.py file.

Does that structure look familiar? It should. In the last lesson, you created a Python package by hand. Now uv has created the same kind of package structure for you.

pyproject.toml

Take a closer look at pyproject.toml. Here it is as uv generated it when this walkthrough was checked. Your version numbers may differ, and if Git knows your name and email, uv may also add an authors entry:

[project]
name = "review-stats"
version = "0.1.0"
description = "Add your description here"
readme = "README.md"
requires-python = ">=3.12"
dependencies = []

[project.scripts]
review-stats = "review_stats:main"

[build-system]
requires = ["uv_build>=0.12.13,<0.13.0"]
build-backend = "uv_build"

You can see the name of the project, review-stats, and a version number, starting at 0.1.0. Then there is requires-python, which tells Python tools which Python versions this project supports. And there is a section called dependencies. Right now it is empty, because you have not added any third-party packages yet.

You will also see an entry point under project.scripts. It connects the command review-stats to the main function inside your package. So when you run that command, Python knows which function to call.

So you have a project folder, a file describing the project, and a package containing your Python code.

Git and .gitignore

uv also initializes Git for the project by default, so you will see a .git folder and a .gitignore file. Git is a tool for tracking changes to your code over time, and .gitignore tells Git which files and folders it should leave out. This course does not cover Git, but you will come back to .gitignore a little later.

Running the project

Now run the project. Open the package's __init__.py file and you will see a function called main. All it does is print a message:

def main() -> None:
    print("Hello from review-stats!")

Back in the terminal, type:

uv run review-stats

The first time you run this command, uv does a few things automatically. It checks which Python version your project needs. It creates something called a virtual environment. And it installs your project into that environment. The lines it prints describe those steps: which Python it is using, then building and installing your project, with versions, paths and timings from your own machine. Two of the lines are the same everywhere:

Creating virtual environment at: .venv
Hello from review-stats!

The last line is your own message, Hello from review-stats!. Your project is running.

04The virtual environment

But what exactly is the virtual environment that uv just created? Look at the project folder again. There is a new folder called .venv. That stands for virtual environment, and this folder is the key to solving the problem from the beginning of the lesson.

A virtual environmentKey termA folder with its own Python and its own installed packages, for one project: .venv. gives a project its own Python environment, with its own place to install packages. It uses a Python interpreter based on the version you selected, and it keeps the project's installed packages separate from other environments. So instead of every project sharing one collection of installed packages, each project can have its own. If you install NumPy inside this project's environment, it is not installed into every other Python environment on your computer.

Two projects again

Now the original problem starts to make sense. Remember those two projects? One needed NumPy 2.5, and the other needed NumPy 2.3. Each project can have its own .venv folder, so the first project can use one version of NumPy while the second project uses another, and they will not interfere with each other. That is the whole point of virtual environments.

Project and environment

Put those two ideas together:

  • The projectKey termA folder of code with a pyproject.toml that describes it. is a folder containing your code and its configuration.
  • The virtual environmentKey termA folder with its own Python and its own installed packages, for one project: .venv. is where that project's Python packages are installed.

The project describes what you need. The environment is where you actually run it.

05Adding a package

Now install your first dependency. Use NumPy again. In the terminal, type:

uv add numpy

uv figures out which compatible version of NumPy to install and adds it to your project's environment. Among the lines it prints, one names the version it installed:

 + numpy==2.5.4

When this walkthrough was checked, that was NumPy 2.5.4. You may get a newer version.

Predict first

Before moving on, here is a question. You just ran one command. Which files in your project do you think changed? Think it through before reading on.

I have a guess. Show meHide the answer

Two important project files were updated. The first is pyproject.toml. Remember that empty dependencies list? It now contains NumPy:

dependencies = [
    "numpy>=2.5.4",
]

The dependency says that the project accepts that NumPy version or newer, within the allowed version range. So this file tells you which packages the project needs.

But there is another file, called uv.lock. This is what is known as a lock file, and it serves a slightly different purpose. While pyproject.toml describes the dependency requirements, uv.lock records the exact versions that uv resolved. Part of its entry for NumPy looks like this:

name = "numpy"
version = "2.5.4"

Why you need both

Imagine you send this project to someone else. If they only have a dependency requirement that says "this NumPy version or newer", they might end up installing a newer compatible version. But if they use the same lock file, uv can recreate the environment with the same locked dependency versions. That makes your project much more reproducible.

Development dependencies

Not every package you install is something your actual program needs to run. Some packages are just tools you use while developing. Later in this module, you are going to write tests, and you will also use tools that check your code for potential problems. So install three of those tools now:

uv add --dev pytest ruff mypy

Here, pytest is a testing tool. ruff helps you check and format Python code. And mypy checks the type information in your code.

Notice the --dev. It tells uv these are development dependencies. They are still installed in your project's environment, but they are recorded separately from the packages your application needs at runtime. In pyproject.toml, they appear in their own section:

[dependency-groups]
dev = [
    "mypy>=2.4.1",
    "pytest>=9.2.0",
    "ruff>=0.17.3",
]

Your version numbers will likely differ. Because they are recorded separately, someone installing your package as a regular dependency does not automatically need all of your development tools.

06venv and pip: the steps uv automates

If you start reading Python tutorials online, you will probably see people creating environments and installing packages without uv. Two tools are mentioned quite often: venv and pip. Here is what they do.

Creating an environment with venv

Imagine you have another project folder, and you want to create a virtual environment in it by hand. You can run:

python3.12 -m venv .venv

This creates a .venv folder, just like the one uv created for you. It gives you the same basic kind of isolated Python environment.

But there is one extra step. If you want to use that environment directly from your terminal, you normally activate it. On macOS and Linux, you write:

source .venv/bin/activate

Once the environment is activated, your terminal uses its Python interpreter by default.

Installing packages with pip

What about installing packages? That is where pip comes in. pip is Python's package installer. For example, run:

python -m pip install numpy==2.3.5

It installs NumPy version 2.3.5 into the Python environment you are currently using. The last line of its output confirms it:

Successfully installed numpy-2.3.5

So now, in this second project, you have NumPy 2.3.5. But the review-stats project still has the NumPy version uv installed for it. Two different projects, two different versions, and both can work independently.

What uv does for you

So what is the difference between this approach and uv? uv handles several of these steps for you. When you use uv run, it runs a command inside the project's environment without you having to activate that environment manually. And when you use uv add, it installs a package and records the dependency in your project configuration and lock file. With pip, installing a package does not automatically update pyproject.toml or create a lock file for you.

So throughout this course, you will mainly use uv. When you see a tutorial using pip install, remember that uv add is the command you will generally use to add a dependency to your project.

You might also come across a tool called Conda, especially in data science tutorials. Conda is another way to manage environments and packages. This course does not use it, but it is good to know what it is.

07Source layout, VS Code and Jupyter kernels

Now that the project is set up, bring in the code you wrote in the last lesson. Remember text_tools.py? That is the file containing your three functions. Copy it into src/review_stats, right next to the __init__.py file:

review-stats/
└── src/
    └── review_stats/
        ├── __init__.py
        └── text_tools.py

Now your package contains the same text-processing functions you were using in Colab.

Checking it still works

Make sure they still work. In the terminal, you can use uv run python -c. The -c option tells Python to execute the code you provide as a string. So write a short command that imports tokenise from review_stats.text_tools and calls it on a short review:

uv run python -c "from review_stats.text_tools import tokenise; print(tokenise('Easy to clean!'))"
['easy', 'to', 'clean']

You get the same three words: easy, to and clean. Your code works exactly as it did in Colab. The difference is that now it is part of a proper Python project on your own computer.

VS Code

Running everything from the terminal is useful, but you also need a comfortable place to write and edit your code. That is where VS Code comes in. VS Code is a free code editor.

Open your review-stats project folder in VS Code. On the left, you can see all the files and folders in your project. When you open a Python file, you can select which Python interpreter VS Code should use.

This is important. You want to select the Python inside your project's .venv folder. That way, when you run your code from VS Code, it uses the same environment where you installed NumPy and your other dependencies.

Notebooks with ipykernel

What if you still want to use notebooks? You can do that too. Add one more development dependency:

uv add --dev ipykernel

A notebook sends its code to a kernel, the process that actually runs the code and sends the results back. ipykernel is what allows a Python environment to run code as a Jupyter notebook kernel. Once it is installed, you can create a notebook in VS Code and select your project's .venv environment as the kernel.

Now your notebook can import from review_stats, just as you did in Colab. But this time, the notebook and your Python files are all part of the same local project.

08Interpreter mismatch, and never copying dot venv

There is one common problem you should understand, because sooner or later you are probably going to run into it. It is one of the most confusing things for beginners when they start working with virtual environments.

Imagine you have installed NumPy in your review-stats project. You know it is there. You have already used it. But now, instead of running Python through uv, you open a terminal and use the computer's system Python:

python3 -c "import numpy"
Predict first

What do you think happens? Remember, this is not necessarily the same Python environment that your project uses. Think it through before reading on.

I have a guess. Show meHide the answer

If that Python does not have NumPy installed, you get a ModuleNotFoundError:

ModuleNotFoundError: No module named 'numpy'

It says there is no module named NumPy. But didn't you just install NumPy? Yes, you did. You installed it inside your project's virtual environment. But the Python you are running now is a different interpreter, and that interpreter does not have NumPy installed. So there is no contradiction here. NumPy exists in one environment, but not in the other.

The same problem in notebooks

The same thing can happen with notebooks. You might install a package successfully, open a notebook, and still get a ModuleNotFoundError. Very often, the problem is simply that the notebook is using the wrong kernel.

So whenever you get an import error for a package you know you have installed, check which Python environment is actually running your code. That is usually the first thing worth investigating.

Never copy .venv

There is one last detail about virtual environments. Remember the .venv folder? It is created for a particular environment on a particular machine, and it can contain paths and files that depend on that machine. So you generally do not copy .venv between computers or include it when sharing a project. That is why .venv is listed in your .gitignore file:

# Virtual environments
.venv

Instead, you share the project files, including pyproject.toml and uv.lock. Then, on another computer, you run:

uv sync

uv creates or updates the virtual environment using the dependencies recorded in the lock file. So even if you delete .venv, you can recreate it, and NumPy is installed again at the locked version. Among the lines uv sync prints are these:

Creating virtual environment at: .venv
 + numpy==2.5.4

Your locked version may differ, but it will be the same version uv add installed in your project.

That is one of the biggest benefits of managing dependencies this way. You do not have to share the entire environment. You just share the instructions needed to recreate it.

09Recap and challenge: uv, virtual environments and dependencies

You started by moving away from Colab, because you are now building a project that needs a permanent folder for its code and other files. Then you looked at a common problem: different projects might need different versions of the same package. You solved it with virtual environments.

  • You installed uv, used it to install Python, and created the review-stats project.
  • pyproject.toml describes the project and its dependencies.
  • uv.lock records the exact versions needed to recreate the environment.
  • uv creates a virtual environment inside .venv, keeping your project's packages separate from other projects.
  • You compared that with the traditional approach using venv and pip.
  • Finally, you brought your own Python module into the project, opened it in VS Code, and learned how to run code and notebooks using the correct Python environment.

The most important thing to remember is this: installing a package and running Python are two separate things, and they need to happen in the same environment. That is why you use uv run, or select the correct interpreter or notebook kernel.

Your challenge

  1. Create a second project called step-stats.
  2. Add pandas as a dependency.
  3. Run a short Python command that imports pandas.
  4. Once that works, go back to your review-stats project and check whether pandas is available there.

If you have not installed pandas in that environment, it should not be available just because you installed it in step-stats. That is the whole idea of environment isolation. Give it a try.

Your project is set up, and your Python package is in it. But something is still missing. So far, all of your review text has been written directly into your code. In a real project, that data usually comes from files. So the next lesson shows how to read reviews from a file, process them with your functions, and write the results back out to files of their own.