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

Building a Package With uv

00:00 Now that your command line interface is working, you’ll want to probably share it with others. You’ll want to distribute it. And in order to do that, you first need to go through a process called the build process.

00:13 You need to build your package so that you can share it with others. And once you build your package, you will get a couple of files in different formats that are then shareable.

00:25 You can upload them, for example, to PyPI, the Python Package Index. And people can then install it from there and run it. But what does run it mean? Since you have a CLI, you probably want to specify a command with a nice name that users can run.

00:42 For example, you could call your CLI rpcats. Since the project is already called rpcats, it makes sense to pip install rpcats and then run the commands rpcats with the breed name.

00:54 But when the user types rpcats and then specifies for example the Persian breed, how does that connect to your code? How does that work? Well, there’s actually this entry point configuration that you can specify so that when users run the command rpcats, they know to look for a specific function to run.

01:16 And in order for this to work, you need to define these entry points in your pyproject.toml file. So go ahead and open your file pyproject.toml

01:29 and once you’re there, create a new section called project.scripts. And in this section you can specify commands and map them to specific functions in your code.

01:42 For example, we want the command rpcats to run something in the file main and more specifically the main function.

01:51 So you type main:main. The first main specifies the file and the second main, the function name.

02:00 So you save that and now you could think that you could already run your rpcats command, but you can’t because you haven’t built your package yet.

02:09 It’s only when you build it that you have this thing that knows what the rpcats command is. So you’ve specified it here, you’ve configured it, but now you still need to build your package.

02:24 And in order to build your package, you might have asked it, you need to use the command uv build. And this build process depends on a build system.

02:35 So there’s actually some other code that will take care of the building process, and there’s a couple of different build systems. And if you already know what you’re doing, feel free to pick whichever one you prefer.

02:47 But for now, what I suggest you do is open the file py project.toml, and once you’re there, make sure you paste these three lines.

02:57 You’ll want to create a new section called build-system.

03:02 And this build-system has a key called requires that depends on a couple of dependencies.

03:10 And for this course, you’re going with setuptools and wheel.

03:18 And then you’ll specify the key build-backend, which is equal to setuptools.build_meta. And once you have this, feel free to save your pyproject.toml.

03:35 Open your terminal and run the command uv build.

03:41 And it’ll output a couple of things on your screen. And once you do that, you will see that at the bottom it tells you that it’s successfully built a couple of different files in a folder called dist.

03:53 So if you list the contents of your directory, you will see that you have a new folder called dist. And if you list the contents of the folder dist,

04:04 you will find a wheel file and a zipped file with your source builds. And you will also find a new file with some information that you don’t really need to worry about.

04:18 And now that your project was built, you can test the entry point. So you can run uv run rpcats and you can specify a cat breed. And this works. uv figures out that you’re trying to use your own commands that you already built.

04:36 And now as a quick demonstration that these names need to be adapted to your specific projects, let’s go ahead and as a final test, open the file main.py.

04:52 Once you have the file main.py open, go ahead and look for the function main(). And once you find it, rename it as something else.

05:02 For example, my_main_function(). And then make sure that you also change it

05:09 at the bottom of the file. my_main_function(), for example, so this is a different name, a longer name. Now, if this were your file from the get go in your pyproject.toml file, in your entry points inside the section project.scripts, you need to update the key and the value.

05:28 So the rpcats is the name of the command and what’s inside the string is kind of a path to the thing you want to run. And after the colon is the function name and the function is no longer called main(), it’s called my_main_function().

05:42 So you would need to update it.

05:45 Now if you open your terminal, let me clear the screen, you can use the command uv build to build your package again. And now if you run uv run rpcats, this still works.

06:03 So you’ve renamed your function, but this still works because you’ve also updated your entry points, which in hindsight would’ve been more pedagogical if we first built it without updating the entry points to see it no longer works, and then updating the entry points.

06:18 So you can go ahead and do that.

06:21 And another thing you can try to change is change the name of the command. So instead of calling it rpcats, you could call it catinfo.

06:30 And if you save your pyproject.toml, and if you open your terminal, if you rebuild with uv build, now you can run the command catinfo with uv run catinfo Persian,

06:45 and you get some information about the Persian breed.

06:49 So I’ll go ahead and I’ll undo all of this. This just goes to show that you can modify this configuration to suit you. And in different projects you will have different functions, different entry points, different commands.

07:03 You can even have multiple commands if inside the section project.scripts you specify other commands. So it’ll be entirely up to you and dependent on your specific projects.

07:15 So this is how you build your package. But this was very manual, prone to errors. There’s even this line requires, this isn’t even very good because I refrain from specifying versions here.

07:29 So there’s no version constraints. So you actually don’t know what setuptools version or what wheel version was used. And it’s very easy to forget what the section project .scripts is called.

07:40 So in the next lesson, you’ll learn a more automatic way of going through this process.

Become a Member to join the conversation.