Python Tutorial: How to Publish Your Own Python Library Step By Step
By the end of this tutorial, you’ll be ready to publish your own Python library!.
As a Python developer, sooner or later you’ll want to create and publish your own Python library. Doing so can be a great way to share your code with the world, and can also be a helpful way to make money as a developer.
In this tutorial, we’ll walk through the process of publishing your own Python library, step by step. We’ll cover everything from writing the code for your library to packaging it up for distribution to uploading it to the Python Package Index (PyPI).
By the end of this tutorial, you’ll be ready to publish your own Python library!
Writing the Code for Your Library
Before we can publish our Python library, we first need to write the code for it.
For the purposes of this tutorial, we’ll assume that you’ve already written the code for your library and that it is stored in a file called mylib.py.
If you haven’t written the code for your library yet, don’t worry! There are many resources available to help you learn how to code in Python.
Once you’ve written the code for your library, you can continue with this tutorial.
Packaging Your Library
Once you’ve written the code for your library, the next step is to package it up for distribution.
There are a few different ways to package up Python libraries, but we’ll be using the setuptools package in this tutorial. Setuptools makes it easy to package Python libraries for distribution, and also provides a number of features that we’ll make use of later on.
To install setuptools, simply run the following command:
pip install setuptoolsOnce setuptools has been installed, you can create a file called setup.py in the same directory as your mylib.py file. This file will contain the configuration information for your library.
The setup.py file for our mylib library might look something like this:
from setuptools import setupsetup(
name=’mylib’,
version=’1.0.0',
description=’A sample Python library’,
author=’Your Name’,
author_email=’your@email.com’,
url=’https://github.com/yourname/mylib',
py_modules=[‘mylib’],
)Let’s take a look at each of the options in the setup.py file:
name — This is the name of your library. It should be all lowercase and can contain underscores.
version — This is the version number of your library. It should follow the format major.minor.patch, where major, minor, and patch are all integers.
description — This is a short description of your library.
author — This is the name of the author of your library.
author_email — This is the email address of the author of your library.
url — This is the URL of your library’s homepage.
py_modules — This is a list of modules that your library contains. In this case, we’re just including the mylib module that we wrote earlier.
With our setup.py file in place, we can now package up our library for distribution. To do so, simply run the following command:
python setup.py sdistThis will create a file called mylib-1.0.0.tar.gz in the dist/ directory. This file contains our packaged Python library.
Registering Your Library
Now that we have our library packaged up and ready for distribution, the next step is to register it with the Python Package Index (PyPI). Registering our library will make it available for others to install via the pip package manager.
To register our library, we’ll need to create an account on the PyPI website. Once you’ve created an account and logged in, you should be taken to the account management page. From here, you can click on the “Edit Profile” link to edit your account information.
One of the fields on the profile editing page is called “OpenID URL”. This is the URL that will be used to identify you when you upload your library to PyPI.
For the purposes of this tutorial, we’ll assume that your OpenID URL is https://example.com/.
Uploading Your Library
With our library registered with PyPI, we can now upload it for others to install.
To upload your library, simply run the following command:
python setup.py register -r pypiThis will upload the mylib-1.0.0.tar.gz file that we created earlier to the PyPI server. Once the upload is complete, your library will be available for others to install!
Installing Your Library
Now that your library is uploaded to PyPI, others can install it by running the following command:
pip install mylibThis will download and install your library, and all of its dependencies, into their Python environment.
In this tutorial, we’ve walked through the process of publishing your own Python library, step by step.
We’ve covered everything from writing the code for your library to packaging it up for distribution to uploading it to the Python Package Index (PyPI).
By following the steps in this tutorial, you’ll be ready to share your own Python libraries with the world in no time!
Before you leave:
If you liked this article, don’t forget to give me a few claps, follow me and thus receive all updates about new publications.
If you enjoy reading stories like these, consider signing up to become a Medium member. It’s $5 a month, and you’ll receive unlimited access to stories on Medium.
So don’t wait — sign up now and start enjoying all that Medium has to offer.
About the author: Alain Saamego: Software engineer, writer and content strategist at SelfGrow.co.uk
Email:[email protected]
Follow me on Twitter if you want even more content.
