Dennis O'Keeffe
4 min readSep 19, 2021
Heading image

This is Day 21 of the #100DaysOfPython challenge.

This post will use the setuptools package to deploy a package to the Pip package repository.

It will work off the code written in my post “PyTest With GitHub Actions”.

Final project code can be found here.

Prerequisites

  1. Familiarity with Pipenv. See here for my post on Pipenv.
  2. You must have a registered PyPi account.

Getting started

Let’s clone the start repository and add the files and packages we will need to deploy.

At this stage, the repo will now be ready to add our required code to deploy to Pip.

Exploring the current repo

I will use tree -L 2 -a to display the current repo:

What is important for us here to know is that src/math.py contains the code which we will want to deploy and tests/test_math.py contains a simple test for our code.

If we look at the test file, we can see the code we want to deploy contains simple math functions:

We can confirm that this works as expected by running pipenv run pytest.

We can now focus on deploying this as a package.

Altering our files for a package

We need to move the src folder to be the name of the package we want demo-pip-math:

We can now adjust our tests/test_math.py file to reflect this:

You can run pipenv run pytest to confirm everything is as it should be.

Editing our setup.py file

We need to edit setup.py to use the setuptools package and contain information about our package:

Our setup.py makes use of the setuptools.setup function to prepare our package.

Some of the more important arguments we are passing:

| Argument | Does | | — — — — — — — — | — — — — — — — — — — — — — — — — — — — — — — — — — — | | name | Name of the package | | version | Current package version in semantic versioning | | author | Your name | | description | Your package description | | long_description | A description that comes from our README.md file | | python_requires | What versions of Python does your package work with | | keywords | Words to match the package when searching for it |

Add a license

You can choose a license to copy into our LICENSE file. I have gone with the MIT license:

Adding a README

I am adding a simple README for the sake of completing the package deployment. My README.md file reads as follows:

# Your first Pip package in Python

This repository is the companion post to my blog ["Your first Pip package in Python"](https://blog.dennisokeeffe.com)

## Usage

https://gist.github.com/okeeffed/c6a6a61e1a3241836348260a0852d926.js

Include non-Python files in upload

To enure our README.md and LICENSE are included, we can edit our MANIFEST.in file to have the following:

Packaging the files

Let’s edit our Pipfile and add the following to [scripts]:

This will enable us to run pipenv run build to generate our distribution archives and pipenv run deploy to upload our dist/ folder.

In my particular case, if I now head to the provided project link then I can see my package has now been deployed!

Package on PyPi

Testing out the package

To test out the new package, you can create a new project and install:

Add the following code to main.py:

Run python main.py:

Great success!

Summary

Today’s post demonstrated how to use the Pillow package to add text and icons layers to an image programmatically.

This will be the technique I use for helping to build out my blog post main images (including this one!).

Resources and further reading

  1. The ABCs of Pipenv
  2. Pipenv
  3. Choose a license
  4. MIT license
  5. Test repo for installing new package
  6. “PyTest With GitHub Actions”
  7. Final project code
  8. Register PyPi account
  9. Start repository

Photo credit: teodosiev

Originally posted on my blog. To see new posts without delay, read the posts there and subscribe to my newsletter.

I write content for AWS, Kubernetes, Python, JavaScript and more. To view all the latest content, be sure to visit my blog and subscribe to my newsletter. Follow me on Twitter.

Dennis O'Keeffe
Dennis O'Keeffe

No responses yet