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

Exploring Other Build Systems

00:00 In the previous lesson, I did a deeper dive on the available configuration options in your pyproject.toml file. In this lesson, I’ll briefly demonstrate two alternate build systems, Flit and Poetry.

00:14 Flit is an alternate build system you can use instead of setuptools. It’s simpler than most of the alternatives and doesn’t support things like C extensions. Because of this limitation, it means you have far fewer things to worry about for your project and its configuration. It’s not compatible with setuptools, so you need different entries in your pyproject.toml file.

00:35 I’ll be showing you a new file that’s not the one in the reader repo. Like most packages—a bit of foreshadowing there on most—Flit is installed through pip. Let me show you what it looks like to use this tool.

00:50 Once you’ve got Flit installed, to get going, you use flit init. It will ask you some questions. First off, the name of the module, which it detects from the directory if you’re already in the package folder.

01:03 I’ll accept the default. Then it wants who you are, where you can be reached,

01:14 where the project lives,

01:20 and asks you for your choice of license. Once you’ve done all that, it generates a pyproject.toml file. It looks like this.

01:32 Note that this is just the base. It doesn’t have the dependencies in here or the scripts, so in the case of reader, you’d have to add some additional info.

01:40 Like the example I mentioned in the previous lesson, Flit uses the dynamic field. Here it’s saying it’ll generate the version and description for you based on an expected __version__ value and a docstring inside of __init__.

01:59 Once you’ve got the pyproject file created and customized, you simply use flit build to build the package and, once built, flit publish to publish it.

02:11 Another popular build system is Poetry. It’s a bit more complicated than Flit and includes a very powerful dependency management feature. If you’re dealing with a larger project with a lot of interrelated dependencies, Poetry just might be the tool for you. Like Flit, it isn’t compatible with setuptools and needs its own stuff in the pyproject file. And although you can install it using pip, the docs suggest against it. It has its own custom installer.

02:39 My understanding is they’ve done this to try and minimize the chance of package conflicts between the poetry tool and your own project. Let’s go see the Poetry basics.

02:51 If you’re starting from scratch and don’t even have a project structure yet, the poetry new command will generate the directory and give you some skeleton files. If, like us, you’ve already got a project structure, you use the poetry init command instead.

03:09 Like Flit, it asks you some questions. It takes a guess at the name. Note the default here. Flit looked in the source directory, and so it saw a reader. Poetry looks at the package directory, which I’ve renamed so that I can demonstrate the three different build systems.

03:24 I’ll stick with the default here. Now a version number. Let me correct that. And a description …

03:36 author. Yep, I’m pretending to be Geir Arne again. Don’t tell him I stole his identity. A license prompt. I’m going to stick with nothing for now. And a question about what version of Python.

03:51 You can have it interactively ask about dependencies, but you’ve seen those before, so I’m going to skip this. And likewise, for dev dependencies. The resulting pyproject file gets printed to the screen.

04:06 If you’re happy with it, you say yes, and it generates it for you.

04:14 And here it is. Notice how the build-system section is poetry rather than setuptools. And the metadata is in the tool.poetry section.

04:25 This allows Poetry to use whatever attributes it wants rather than having to stick to the PEP definition. This is, again, sort of a history thing. Poetry’s been around for a bit and has been trying to solve some of the problems that are now solved in setuptools but weren’t before.

04:41 Like with my Flit example, there’s a bunch of missing stuff here. I skipped the dependency steps and didn’t do anything about the script callouts. So this isn’t a feature-full example, but you get the idea.

04:55 Like I said, the TOML’s a bit different because Poetry predates PEP 621, but the commands are very similar to Flit. poetry build to build and poetry publish to publish.

05:08 An entire course could be devoted to the ins and outs of Poetry. This was more of a taster, so you could see there were alternatives to setuptools.

05:15 There are plenty of choices when it comes to packaging in the Python world, whether it’s setuptools, Flit, Poetry, or something else, hopefully you can find tools that meet your needs.

05:27 Last up is the course summary, and as usual, that includes some references for further investigation.

Become a Member to join the conversation.