How to Build Your First Python Package

Step 1: Choose Your API

#1: Public and Private Methods

Pro tip: You can use the special variable __all__ to define exactly which functions to expose and which not when the users use from your_package import *. Check out this thread on StackOverflow for more information.

#2: Adding __init__.py

your_package |-- module1.py |-- module2.py

You’ll need to add another file, __init__.py to your package in order to make Python interpret the your_package directory as a Python package. This means now it should look like this:

your_package |-- __init__.py |-- module1.py |-- module2.py 

You can leave the file empty as a starting point, but it has to be there in order to allow from your_package import module1.
Pro tip: The code in __init__.py is executed once your package is being imported, and this allows you to do all sorts of cool stuff. For example, you can use __init__.py to shorten your API. Let’s say that the most important method of your code, imp_func, is found in module1 . You can allow the users to import it as from your_package import imp_func instead of from your_package.module1 import imp_func, by simply adding:
from .module1 import imp_func

to your __init__.py file.

Step 2: Document

#1: Docstrings

  • Summary: Explain in a simple language what the function suppose to do
  • Parameters: Explain what are the parameters and parameter-types which the function expects
  • Return value: Explain what the function returns
  • Example: While not a must, it is always useful to add a usage example
Here’s an example:

#2: Read-Me
Read-Me files are usually written in a format known as MarkDown, which you can learn more about here. This is why they’re usually named README.md.

Step 3: License

At this point, your package is supposed to look like this:


your_package |-- README.md |-- LICENSE |-- __init__.py |-- module1.py |-- module2.py

Now we’ll add the file that builds an installable Python package out of your code. For this, you’ll have to add a new file named setup.py, and rearrange your files in the following way:
your_package |-- setup.py |-- README.md |-- LICENSE |-- your_package |-- __init__.py |-- module1.py |-- module2.py

The setup file dictates everything Python installer needs to know when installing your package. A very basic one which you can simply copy-paste, looks like this:


Except for editing your personal info, there are two things you’ll have to keep track of:
  • Version: Each time you’ll release a new build to PyPI (which we’ll discuss in a second), you’ll have to bump your version number
  • Install requires: This is a list of all the external dependencies of your package. Be sure to list everything here, so Python will install them along with your package

Step 5: Sign-Up to PyPI

Step 6: Build & Deploy!

Before we begin, we’ll need to install twine, which will allow us to deploy to PyPI. It is as simple as:
pip install twine
Next, we’ll create an installable package. Go to the directory where the setup.py file is, and run:
python setup.py sdist bdist_wheel
This will create several new directories, such as distbuild and your_package.egg-info. It’s dist which we care about now, as it contains the installation files we want to deploy to PyPI. You’ll find two files in there: a compressed tar file and a wheel file. And in a second, they’ll be available to the whole world.
Pro tip: Add these directories to your .gitignore file, to prevent pushing installation files to your repo. (Never heard of .gitignoreRead this)
Next up, verify the distribution files you just created are okay by running:
twine check dist/*
Time to upload your package to PyPI. I recommend deploying first to the PyPI test domain, so you can verify everything looks as you intended. Do this using:
twine upload --repository-url https://test.pypi.org/legacy/ dist/*
Go to test.pypi.org and check your new library. Satisfied? Great, let’s push it to the real PyPI:
twine upload dist/*
That’s it. From now on everyone can install your package using pip install. Great job!
Pro tip: You can make your life easier with a Bash script, which will build, check and deploy your package with a single command. Create a new file named build_deploy.sh in the same directory as setup.py. Copy-paste the following code into this file:
Now all you need to do is run:
./build_deploy.sh
From the directory where the file is at, and that’s it. You can also run:
./build_deploy.sh --test
to upload to the test domain. (Note you’ll have to make the script executable before running it the first time. Simply run chmod +x build_deploy.sh in the directory where the file is located.)

The Extra Mile: Write a Blogpost

Final Words

Post a Comment

0 Comments