We'll install so many packages in our day to day python development. Now in this blog post, we'll try to know how to create our own python module installable with PIP. In this blog post we'll make our own package installable with PIP using PyPI. PyPI is the default Package Index for the Python community. It is open to all Python developers to consume and distribute their distributions.
To make a python module installable with PIP via PyPI the follwoing files are to be included in your project:
This is the major file that makes our normal python module to an installable package. The major part of setup.py is that it contains a global setup() function. The keyword arguments those being passed to this function define some specific details of your project like name, version, description etc., The following are the few arguments that we pass frequently to the function.
name='django-simple-pagination'This is the name of your project, and will determine how your project is listed on PyPI. For name should follow PEP426 naming convensions.
version='0.1'This is the current version of your project, allowing your users to determine whether or not they have the latest version, and to indicate which specific versions that the users can install. Versions are displayed on PyPI for each release if you publish your project.
description='A simple pagination app for Django' long_description=READMEThese will give short and long description for your project. These values will be displayed on PyPI if you publish your project.
url='https://github.com/MicroPyramid/django-simple-pagination'This is the url where your projecct code is placed, generally it is a git url for your project.
author='Ashwin', firstname.lastname@example.org'Provides details about the author like author and author email.
classifiers=[ # How mature is this project? Common values are 'Development Status :: 3 - Alpha', # Indicate who your project is intended for 'Intended Audience :: Developers', 'Environment :: Web Environment', 'Framework :: Django', 'Topic :: Software Development :: Build Tools', # Pick your license as you wish (should match "license" above) 'License :: OSI Approved :: MIT License', # Specify all Python versions you support here. 'Programming Language :: Python :: 2', 'Programming Language :: Python :: 2.6', 'Programming Language :: Python :: 2.7', 'Programming Language :: Python :: 3', ],For a full listing, see https://pypi.python.org/pypi?%3Aaction=list_classifiers.
packages=['simple_pagination', 'simple_pagination.migrations', 'simple_pagination.templatetags'] or packages=find_packages(exclude=['contrib', 'docs', 'tests*']),Packages is the list of the packages to be included in your project. They can be listed manually or find using setuptools.find_packages automatically. Use the exclude keyword argument to omit packages that are not intended to be released and installed.
install_requires=['django']“install_requires” is the list of modules what dependencies a project minimally needs to run. When the project is installed by pip, these will be installed automatically.
include_package_data = TrueThis tells the sdist command whether to include package data or not.
All projects should contain a readme file that covers the brief documentation of the project. The most common format is reStructuredText with an “rst” extension.
A MANIFEST.in file can be added in a project to define the list of files to include in the distribution built by the sdist command. When sdist is run, it will look for the MANIFEST.in file and interpret it to generate the MANIFEST file that contains the list of files that will be included in the package. This mechanism can be used when the default list of files is not enough. The following is the simple MANIFEST.in file.
include README.rst recursive-include simple_pagination/templates *The recursive-include tells sdist to include all files anywhere under the simple_pagination/templates directory.
After adding all the above files in your app, now your project is ready to be pushed into PyPI.
Uploading our project to PyPI:
- Create an account in PyPI.
- Reguster your app to PyPI using following command.
python setup.py register -r pypi
- Upload your distribution to PyPI using the follwoing command.
python setup.py sdist upload -r pypi
With this proceedure we can make our app to be installable as a python package.
Note: If we edit and try to push the code it won't accept, we have to change its version for every code push.