Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Adding Dependencies With uv

00:00 Now that you’ve created your rpcats project, it’s time to see how you can use uv to manage dependencies. Because more often than not, when you’re creating a project, you will require dependencies.

00:13 You will need to install other packages that you will build on top of. And to demonstrate that, we are going to be using a short command-line application that is already built for you.

00:25 You’ll just download this file in a minute. And once we do that, we’re going to use two uv commands to manage dependencies: uv add and uv remove.

00:36 And I’ll let you figure out which one is going to be used to add dependencies, and which one is used to remove dependencies. In order to be able to use these two commands, you’ll need to download a short script called rpcats.py.

00:50 That’s available as a download for this course. So go ahead and make sure you download that.

00:58 Once you’ve downloaded that, you should have a folder called materials, which contains the script rpcats.py. I’m going to list the contents of the directory materials and you should find the rpcats.py inside.

01:15 And at this point, the downloaded folder is next to the project folder you created in the previous lesson.

01:23 So if you list the contents of the folder rpcats/,

01:28 you have the files that uv created in the previous lesson for you. So what you will want to do now is copy the contents of the script rpcats.py.

01:40 You want to take the contents of that file and paste it inside the main.py of your uv projects. Feel free to do that in the way that’s easiest for you.

01:50 You can open the file, select everything, and copy and then paste. You can move the files around, rename the files. You do whatever is easiest for you. What I will do is I will use shell commands to move the file there, or actually I’ll just copy it.

02:06 So you copy the rpcats.py inside the materials folder into the

02:11 rpcats projects, and you name it as main.py.

02:16 And once that’s copied, you’ll want to go into your rpcats projects. And then let’s take a look at the small program that we’re using as the projects. So go ahead and open that file in your editor of choice.

02:35 Once the file is open, you should see a couple dozen lines that look like this. It’s importing a couple of modules from the standard library. It’s importing a couple of modules that are not from the standard library, and then it defines some functions

02:49 and it ends with a call to main.

02:53 So this is what your code should look like, the code that you downloaded. And it’s not very important to understand everything because in a second you will see what this code does.

03:04 So what you can try to do now is open your terminal again

03:08 and you can try to use the command uv run main.py to run this program, the program you just downloaded, and it’ll not work because the script is trying to import the module requests, which you haven’t installed.

03:23 So this is where the uv add command comes in. If you type uv add requests, uv will add the dependency requests to your project.

03:35 Go ahead. You press Enter, you give it a second, it’ll do a couple of things. The first thing that will do is it’ll update the py project.toml file.

03:45 If you open the file pyproject.toml, you will see that there’s this key dependencies that has a list value. And the list now contains requests, and it’ll contain the name of the module or the package that you try to install.

04:02 And the version constraint that uv determines is what you needed. By default, it’ll try to install the most recent one. And uv did not only do that, but it also updated the file uv.lock.

04:17 If you go ahead and you open the file uv.lock, you will see that it now contains a bunch of lines that you did not put in there.

04:26 And again, that’s how it’s supposed to be. The file uv.lock is edited automatically by uv and you’re not supposed to modify by hand.

04:37 Once uv does this, uv will also install requests into your virtual environment. And now open your terminal again and you can try to run your script again.

04:47 So if you type uv run main.py,

04:51 now it no longer complains about requests, but you will see it complains about rich because we are also trying to import it and we did not install rich.

05:02 So you already know what to do. You type uv add rich, you give it a second, and then uv will do everything we discussed. It’ll update the pyproject.toml file.

05:14 It’ll update the uv.lock, it’ll install rich into the virtual environment. And now finally, if you run your project with uv run main.py, it should work, and it works.

05:27 You are running your CLI, you just need to use the CLI properly. And this example program you’re using is a command-line interface that gives you information about cat breeds.

05:39 So the way you use it is you specify a cat breed as an argument. In order to do that, let’s just clear the screen so there’s less noise. In order to do this, you use the uv run command.

05:54 Now you want to run your main.py file and you need to pass it the argument of the breed you want to learn about. So for example, let’s learn about Persian cats.

06:03 So you give it a second, and then you see some information on the screen. You see the origin of the breed, the temperament, etc. So this is working. Now there’s two things you need to learn.

06:14 The first one is that when you looked at the file, you saw that we were importing requests and rich. So you could have installed both in one go by using the command uv add, and then listing the two dependencies directly.

06:30 So uv add accepts as many packages as you want as arguments, so you could have installed both at the same time. But now here’s the thing, the dependency on rich isn’t really required.

06:42 You added rich as a dependency, but it turns out you don’t need it. You can assume that you were using rich to create some fancy output, but then you eventually got rid of it.

06:53 So in the next lesson, you will see how to use uv to remove the dependencies that are no longer required.

Avatar image for jakubmg

jakubmg on Oct. 1, 2025

Hi Rodrigo, At this video, what I have on the computer differs to what you are showing. I assume there was an update to the sample code, as it was not packaged for me in additional folders. More importantly the main.py file does not execute the sample code. I assume I need to alter the main.py contents to run the code, right?

Avatar image for Tappan Moore

Tappan Moore RP Team on Oct. 6, 2025

Hi @jakubmg, you’re right. I have changed the sample code zip so that it matches the lesson. Thanks!

I can’t speak to the last part, perhaps Rodrigo or others can chime in.

Become a Member to join the conversation.