Building a Python Package: First Steps (2)

by Jarrett Retz January 7th, 2021
python programming building a python package

Since we are looking to eventually publish the package on PyPi.org, I decided to start looking for a guide on publishing on the site.

I quickly came across an FAQ page that had links to a packaging tutorial on the Python Packaging User Guide.

Following the link, I ended up on a subdomain of python.org. This looks like the right place!

Installing Python

First, let's make sure that we have the latest version of Python installed.

Go to https://www.python.org/downloads/release and click on the Download Python 3.9.x. This may automatically detect the proper OS for your download.

Open the .pkg file and work through the installer. I am using a Mac so your installation process may be slightly different.

Next, after the installation is complete, open up a new terminal or command prompt.

Run the command python3.

Python's interactive command shell should open and the latest Python version should appear in the first line.

Update & Install Needed Packages

We need to run a quick command that is important for packages that are used when building and packaging our package.

python3 -m pip install --upgrade pip setuptools wheel

Next, we need to install pipenv.

pipenv is a package that let's us track the dependencies of our package. It allows use to create a virtual environment.

You can read more about pipenv on the Managing Application Dependencies tutorial page.

To install pipenv, run the command pip3 install --user pipenv.

Create Project Folder

Create a new folder for our project. I'm going to name mine bea. I think I'm going to create a package for the Bureau of Economic Analysis API. There are a couple out there, but I'm not particularly fond of them.

Open up your text editor or IDE. I am using Visual Studio Code.

Inside the main project folder, create files with names:

  • LICENSE
  • README.md
  • setup.py

Next, create two folders:

  • bea
  • tests

The folder structure will look like the below tree.

.
├── LICENSE
├── README.md
├── bea/
├── setup.py
└── tests/

Many of these files won't be used till later.

The final file we are going to create in this project setup is an __init__.py file inside of the nested bea directory.

.
├── LICENSE
├── README.md
├── bea
│   └── __init__.py
├── setup.py
└── tests

That's it for this section. We are now in a comfortable position to start developing the package.