Writing Your First pyproject.toml
In this lesson, you’ll create pyproject.toml
, replacing setup.py
and setup.cfg
along the way. You’ll also learn about reading TOML.
00:00
All right, so we want to create our package in the relatively new way to create packages now. Yes. So, how do we do that? So this now all is defined in a file called pyproject.toml
.
00:13
Let’s create the file in the top directory, above snakesay
. Above snakesay
? Okay, interesting. So we’ll kind of keep the code in one place, and then we’ll keep the definition of it one directory up. Okay, pyproject.toml
. Yes.
00:29
And what this file does is that it contains information about Python projects. That’s kind of where the name comes from, and the toml
there is a file format that is a configuration file that is typically fairly readable. And we’ll see that it’s mostly intuitive.
00:45
So what this replaces is the setup.py
and setup.cfg
files that we would have before. Right. If you’ve done some packaging earlier, you may have seen things like setup.py
, as you say, setup.cfg
, and files like this.
01:00
We don’t need those anymore. We can just use pyproject.toml
. Just the one file, that’s fantastic. Yeah. And also, if you have other Python tools helping you with development, those can usually also be configured in the same file, so things like black
and—Uh huh, so you can set your line length in pyproject.toml
along with all the other configuration elements for your project. That’s really cool. Yes.
01:23
So to have a look at how to actually get started with this, there are a couple of places to look. Let’s see. Yeah. So on the setuptools homepage, they have a section, Configuring setuptools Using pyproject.toml
Files.
01:38
And one thing you can just see on the top there is that this is fairly new. It’s new in some version, but it’s here, and then you can see that if you scroll little bit to the bigger text block there, so you can see here that it says things like build-system
, project
, and optional-dependencies
, scripts
, and things.
01:57 So this is definition of our project.
02:02
So these are containing namespaces, and these are the values within that. So you can kind of think of it like a dictionary. Yep. Like this would be a dictionary called project
, although it’s not a dictionary—it’s a toml
file. But this is a way to think about it. Yeah, completely.
02:19 If you parse this into Python, your end result is typically a nested dictionary. Okay.
02:26 And if you’re interested in more TOML, Geir Arne has another article called Python and TOML: New Best Friends, where he gets very deep into everything that TOML entails. Right. But yeah, in general, the syntax is very similar to Python.
02:39 So you can kind of more or less guess what at what it’ll— Yeah, TOML kind of won out for this because it’s very easy to read, right? It doesn’t have many extra characters like something like JSON has. Exactly.
Become a Member to join the conversation.