-
Notifications
You must be signed in to change notification settings - Fork 2
Creating a Python Library
If you have used Python even for only a short while, you are likely to have imported some library using the import keyword. In this short wiki page, we are going to go over how you can convert your own Python files into a library that others could eventually install and use.
Python libraries are created by pushing them to an index called the PyPI(Python Package Index). It is simply a repository of packages for Python. You install a library from PyPI using:
pip install <library_name>
Head to PyPI's website and create an account.
You need to structure your code in a particular format to be able to create a library. I will be explaining this format and proceeding steps with this repository as an example since this repository is used to update a library called easygit.
Here is the file structure to follow:
- Library name
__init__.py- Other Files that implement library logic (Ex:
.pyfiles)
setup.py- LICENSE
- README.md
Let us have a look at what each file is to contain:
This file is executed automatically when you import the package. So, include all relative imports here which might be useful when using the library. For example, in the reference repository I mentioned earlier, you can find from .easygit import Easygit within __init__.py. This is because Easygit is the name of the class inside easygit.py which implements all the core logic and thereby has been called upon by making a relative import.
Ex:
from setuptools import setup, find_packages
import codecs
import os
here = os.path.abspath(os.path.dirname(__file__))
with codecs.open(os.path.join(here, "README.md"), encoding="utf-8") as fh:
long_description = "\n" + fh.read()
VERSION = '1.0.3'
DESCRIPTION = 'Library to learn git'
LONG_DESCRIPTION = 'A package that allows to query git commands easily'
setup(
name="easygit",
version=VERSION,
author="Sharan Babu",
author_email="sharanbabu2001@gmail.com",
description=DESCRIPTION,
long_description_content_type="text/markdown",
long_description=open('README.md').read(),
packages=find_packages(),
install_requires=['wit', 'colorama'],
keywords=['python', 'chatbot', 'git', 'interactive terminal'],
classifiers=[
"Development Status :: 5 - Production/Stable",
"Intended Audience :: Developers",
'Topic :: Software Development :: Build Tools',
'License :: OSI Approved :: MIT License',
"Programming Language :: Python :: 3",
]
)This file contains important metadata about your library. Almost everything in this section is self-explanatory as you just have to replace the field values with your own. For Example, You would change the 'name' parameter within the setup() function to the name of your library. Do check that the name you are entering is available. Similarly, you have 'description' and 'long_description' which will be displayed on the PyPI page after your files have been pushed to PyPI. Another important line in this file would be install_requires. Here, mention the names of the libraries which will have to be installed in order for your library to work, in other words, mention the dependencies. Fill other fields accordingly.
This file should contain details like description of your library, how to use it and other guidelines, references which may help the user better use your library.
Choose a suitable license for your code/library. You can have a look at the different types of LICENSES here.
Now, you have everything you need to upload your library to PyPI.
Open your terminal, go to the directory which contains all the above files and follow the steps below:
Step 1:
python setup.py sdist
Step 2:
pip install twine
Step 3:
twine upload dist/*
You will now be prompted to enter your PyPI account details (username and password). Enter your details and your package should have been successfully uploaded to PyPI. An URL will be returned in the above step which you can use to view and manage your library.
You can now try installing your own package!
If you are to make changes to your library, follow the steps below once you are down with the changes:
python setup.py sdist
twine upload dist/*
Your changes will be pushed. Users who install the library after these changes will directly see the new code. Meanwhile, the users who already had the library installed, would have to use the following line of code to update their library to the latest version.
python install --upgrade <package_name>
So, that was about how you can create a package/library in Python.
easygit by Sharan Babu and Silvoj Rajesh