Over the past few days - I've been looking into getting more involved in opensource - so I've been on Github just exploring different repositories and technologies. This got me thinking - how cool would it be to have a tool or program that could help other developers up on GitHub? Most of my stuff now is just websites - not full applications. But how do I start? How can I make my software available to others to use? After googling, I quickly realized I needed to make my own package! So here we will learn the basics of how to make your own Python package!
What is a Package
A python package is just a bunch of files that do something. Similar to a python module - which ends up just being a file.py
(module) that can be imported for use.
Minimum Requirements
As I said above, this will go over everything needed at the most minimal level to create your own python package. So let's say you have this awesome idea for a package and you call it my_package
- so original I know 🤣.
File Structure
The below shows the minimum files and directory structure needed.
📦 my_package
┣ 📂 my_package
┃ ┗ 📜 __init__.py
┣ 📂 tests
┣ 📜 LICENSE
┣ 📜 README.md
┗ 📜 setup.py
The root directory is the name of our package - my_package. Then inside this, we have another directory named the same thing. This is where all of the source code for the package will live - Don't forget the init.py file! At the root we also have
- a tests directory for all unit tests
- a LICENSE file so one can know the terms of use of your package - if you need help picking a license just visit HERE
- a README written in markdown. This is where you explain all about your package like what it does, how to use it, how to download etc.
- Last is the setup.py file which is used as the build script by setuptools. This file contains things like the name of the package, the version, description, author, and more.
Generating Release Requirements
After you have the needed files, we need to create the needed tools/files for a release, otherwise known as generating the distribution packages for the package. These tools can quickly and easily be installed with pip
on the command line as follows:
python3 -m pip install --user --upgrade setuptools wheel
after installing these run the below command from the package root directory:
python3 setup.py sdist bdist_wheel
VOILA and just like, that you've generated what is needed for distributing the package.
Upload Your Awesome Package!
Now it is time to upload your package to the Python Package Index
You will need to register for an account (if you don't already have one) on PyPi.
We can use twine to now upload the package. First, you will need to install it if you don't already have it
python3 -m pip install --user --upgrade twine
Then run the below command to upload your package!
python3 -m twine upload dist/*
And just like that you now know what is the minimum needed requirements to upload a package to PyPi and be able to install that package with pip
.
Few Notes
If you have questions on any topic here - take a look at this tutorial as it is what I followed to do so.
Also, note that the above tutorial shows how to also upload to TestPyPi and how to debug and make sure your package installs correctly etc.
Thanks for reading and can't wait to see what awesome package you come up with! Stay coding!
Originally posted on my personal blog: Check it out here!
Top comments (2)
This is informative. Thanks for sharing..
Thanks for taking a look! Glad you got something out of it.