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

Removing Dependencies With uv

00:00 So in the previous lesson you saw how to use the uv command add to add the dependencies, and now you will see how to use the command uv remove to remove the dependencies.

00:14 The rich dependency is actually not required. It’s there, but it’s not required. So it’s just an extra import so that you can see how to remove the dependencies.

00:23 So what you will do is you will use the command uv remove rich and uv will get rid of the rich dependency and all of the transitive dependencies.

00:35 So all of the things that rich depends on that nothing else in your project depends on.

00:40 And let me just highlight how useful this behavior is because again, when you use uv remove rich, uv cleaned up not only the dependency rich, the package you named explicitly, but also packages that rich depends on that are not needed by any of your other dependencies.

01:01 So uv will clean up everything that’s no longer required. That’s what I mentioned as the transitive dependencies of rich. And this is very helpful because some other tools don’t do this by default, and you are left with useless dependencies that you’re not even aware of and uv will clean those up for you, which is very, very neat.

01:23 So that cleans up the uv.lock file, the pyproject file, and your virtual environment. And now if you go ahead and you open the main.py script, you can scroll to the top and you can delete the line that says import rich because that’s not required.

01:41 You save your file, you open your terminal,

01:45 and now you can run uv run main.py and let’s do a version check again.

01:52 And your CLI obviously still works. This is the basics of managing dependencies as far as adding and removing dependencies goes. There’s one other thing that’s very important and that is there are dependencies that you might have which are useful for you as the developer, but not useful for the user.

02:11 For example, a common one would be pytest for testing because as you’re developing your package, you want to have pytest to be able to test your package, but end users probably won’t care about it.

02:24 And uv has a way to specify a dependency as a development dependency. Let’s clear the screen. To add a dependency, you already know you use the command uv add.

02:37 So if you want to add pytest as a dependency, you run uv add pytest. However, if you use the --dev option, uv will add this as a development dependency, which means folks developing the package will have this installed, but users will not need to install pytest and uv will understand that.

03:00 So you run this, you give it a second, and the output looks the same. The difference will be in the pyproject.toml file. So if you go ahead and open that,

03:11 once you open your pyproject.toml, you will see that the pytest dependency was also added but in a different section, whereas the requests dependency is inside the list under the key dependencies, which is under the main header project.

03:28 The pytest dependency is inside the list dev, which is in a different header called dependency-groups. So this is something neat that uv does for you.

03:38 You’re allowed to separate dependencies that are general that your application always requires, your package always requires from the ones that only development users need.

03:49 And a final note regarding these dependencies and things you install as dependencies, if you go ahead and open your terminal in order to run pytest, because pytest is inside your virtual environment, you can use uv run.

04:05 So if you wanted to run your tests, you’ll do something like uv run pytest and uv will be using the pytest it installed to run your tests.

04:14 Now, because there’s no tests, that’s the output. You get no tests ran, but you ran pytest, the one that you installed inside your virtual environment.

04:23 So that’s why the uv run command is very, very important.

04:28 Now you have your CLI application working. You’ve installed the dependencies you require and it’s essentially working. Now what you want to do is you want to turn this into a package that you can easily distribute, and that’s what you will learn in the next lesson.

Become a Member to join the conversation.