In part 2 of this series (this part), we will learn how to create Python Modules and Packages, and how doing so will improve your path to production. You will learn how, and why, to:
- Create a python module.
- Create a python package.
- Further codify your dependencies.
Structuring Directories for Packaging
In the last part of this series, we created a directory called
sample-python-program, we created a virutal environment, added some files, and we downloaded the FastAPI package from PyPi using pip. To continue following this guide, make sure you are inside the sample-python-program directory and have acitvated your Python Virtual Environment.
If you have written Python programs before, you probably appreciated the fact that you could just create a .py file and start adding code right away. A single python file with just one print statement is a viable program that can be ran strait from the command line with no problems. When ever you create larger projects though, the "single file" approach with "every thing in one directory" becomes a bit unmanageable. Changing the structure of your project later on isn't too hard, but can be a point of stress if your program has some level of complexity.
To start, let's create a directory to house our source code, and setup file.
This is where all of the code for our project will live. Inside of the src directory we have created another directory named simple_api. This is the name of our Python program. It's a good idea to name this directory what ever your python package will be called. There are some rules surrounding naming packages and modules that you cand find here.
The other file we created, setup.py, is a crucial piece of the packaging puzzle. Open up your favorite text editor and begin editing
Okay, I think an explanation is in order. The question, why do this? It can be hard to understand the benefit of this if you haven't created a package before. I am willing to bet that you have used a package before though. A simple example is the python datetime package. Let's take a look, start your python interpreter by typing
python at the command line. Once you have started the interpreter run these commands.
When you call
import datetime, you are importing the datetime package. The datetime package contains a bunch of different modules, one of which is also called datetime. At the end of the day, packages are technically just modules that contain more modules, but it helps to think of a package as a collection of modules, or the folder that stores them.
By creating a setup.py file, we are setting forth the intention to package our simple api program. This has many benefits: testing, building for release, extending and reusing our program. Some of these benefits may not be totally clear until you get to the later parts of this series.
Finally, Let's Write our Program
Change directories into our program folder (don't forget to exit the python interpreter by pressing
[ctrl]+d) and create some more files. We will be filling them with code soon enough!
venv have been hidden for brevity):