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
- Familiarity with Pipenv. See here for my post on Pipenv.
- 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!
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
- The ABCs of Pipenv
- Pipenv
- Choose a license
- MIT license
- Test repo for installing new package
- “PyTest With GitHub Actions”
- Final project code
- Register PyPi account
- 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.