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

Unlock This Lesson

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

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set your subtitle preferences in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

Make It Installable

00:00 In the previous lesson, I showed you how to take your code out of the REPL and make it more script-like. In this lesson, I’ll take it a step further and make it installable and runnable.

00:11 Packaging in Python has a rather assorted history and is a bit complicated. The current recommended way of doing things is to use a pyproject.toml file.

00:20 This is a replacement for the older setup.py you may have seen in the past. The pyproject.toml file is a data file used by the Python build system, and it gets used to determine how a package is built, installed, and run.

00:35 If you were going to put your project up on PyPI, this is also the first part of bundling it as well. I won’t go that far here though. I’ll leave you with a local package instead that you can install using pip -e.

00:48 Let’s go look at the pyproject.toml file.

00:53 This is the pyproject.toml file for our RP Life program. The build system section is expected by Python and specifies what engine gets used to build the package.

01:05 Installation requires a package, and this says to use setuptools to build it and output a wheel file. The project section contains a bunch of metadata about the program, including the name and where the README file is.

01:20 The dynamic attribute in the project section tells setuptools what values of this section should be built dynamically for RP Life. The version attribute is the only thing being built dynamically down at the bottom here in the dynamic section for setuptools, I’m telling setuptools how to generate the value for version in this case by reading it from the __version__ attribute in the module.

01:46 Remember when I said putting in the __init__ served a dual purpose? Well, this means your version number only ever has to be specified in one place, and the build system can read it just like argparse can.

02:00 The next chunk of the project section specifies what libraries this code is dependent upon. The string here says it needs tomli, but only if the Python version is less than 3.11.

02:11 If Python is 3.11 or greater, there are no dependencies, as tomllib can be used in that case.

02:19 The final bit here is the magic that makes our code into a script available on the command line. The project scripts area allows you to define one or more programs. I’ve called mine rp_life, and the value here says to use the main() function in the __main__ file of the rp_life module as the entry point.

02:39 Once you’ve used this pyproject.toml file to install your project, rp_life will be available as a directly runnable script. Let’s go do that. I’m creating a new virtual env to install our package into.

02:57 Activating it,

03:02 and then pip install -e with the local directory.

03:10 pip finds the pyproject.toml file here and uses it to locally install the package detailed in that data file into the virtual environment.

03:19 With that done, I can now use the RP Life script hook directly.

03:28 Yep. Still pretty.

03:33 And there you go. You’ve got an installable runnable version of Conway’s Game of Life. In the last lesson, I’ll summarize the course and point you at other similar projects you can do at Real Python.

Become a Member to join the conversation.