PIP packages or PyPI packages are an easy way to install custom reusable python modules or applications that once installed can be used with any other python (django) application, project or module. If you are a python developer you must every now and then have used the
pip install command to install third party python packages. Today in this article I am going to show you how you can create a pip package or a PypI package of your own reusable python or django app/module so that others can easily install it using PyPI with the
pip install your-package command.
Install Required Packages
First let us install the required pip packages. We’ll need the “Wheel” package. So let’s install it by running the following command:
pip install wheel
Create RequiredÂ Files
Now we need to create 2 files in the root of our package directory.
This file will containÂ all the information about your pip package. So create this file in the root of your package and insert the following code. Make sure you replace the values to your package’s values.
# _*_ coding: utf-8 _*_ from distutils.core import setup from setuptools import find_packages setup( name='django-instapush', version='1.0.0', author='Amyth Arora', firstname.lastname@example.org', packages=find_packages(), url='https://github.com/amyth/django-instapush', license='MIT License', description='Send push notifications to android and ios devices', long_description='Django Instapush can be used to send GCM and APNS '\ 'notifications to android and ios devices respectively. This '\ 'package supports both mysql and mongoengine models to store '\ 'device data', zip_safe=False, )
Let’s go through each keyword argument we used in the setup method to define the information for our pip package.
name: This should contain the name of your pip package. This is what users will type in when installing your package using pip. Make sure this name is unique and not already taken and is as close as possible to your actual package folder name.
version: This defines the version of your pip package.
author: Your or Author’s Name (if you are not the author)
author_email: Your or Author’s Email (If you are not the author)
packages: This defines the packages and sub-packages that you would like to include in the installation. If you just have a few you can use an array of the folder names for e.g.
['package1', 'package2']. A better practice is to use the
find_packagesmethod provided by setuptools. What this does is include all the modules that contain a
__init__.pyfile in them so you do not need to list all packages and sub-packages.
url: This represents the url to the repository where the code to your package is hosted. You can use both
githubpublic repository urls.
license: This defines what license you are distributing your package under.
description: This is the short description of your pip package
long_description: This should contain a detailed description of your pip package
zip_false: If you set this to false, it will prevent the package manager from installing the egg and will actually copy the folder, which can be really useful for debugging purposes.
install_requires: This should contain an array of names of any third-party pip packages that your package may depend on. This will make sure these packages are also installed with your package (If not already installed).
Get the package ready for upload
Now run the following 2 command to get your package ready for upload:
python setup.py sdist python setup.py bdist_wheel
sdist: The first command creates a source distribution. (This is unbuilt)
bdist_wheel: This creates a built distribution. If your package is a pure python package and supports python 2 & 3, then use the
--universalflag with this command.
Create an account on PIP (Optional)
This step is required if you do not already have an account on pip. Click Here to create an account with pip.
Submit Package Info to PIP
Now go to the pypi website login and click on the package submission link. In the page that opens, use the 2nd option and click on upload button. Once the dialog opens browse to your package directory and you should find a file named
PKG-INFO. Select that file and click ok and click the submit button to submit the package information for your pip package.
Upload the package to PyPI
In order to upload the package create a file under
~/.pypirc and enter the following code. Replace the
yourpippassword with your pip credentials respectively
[distutils] index-servers=pypi [pypi] repository = https://pypi.python.org/pypi username = yourpipusername password = yourpippassword
Once you have created the file, we are ready to upload the package to pip. To do so simply run the follwoing command.
python setup.py sdist bdist_wheel upload
After this is done, Go back to the pip website to confirm successful upload of your package. If you have any question or queries, do let me know in the comments section below. Regards.