While developing python packages, developers must have a knowledge of setup.py file. In this Python tutorial article we will discuss this setup.py file.
Table Of Contents
Directory of a python package
Here is how a python package looks like:
There is also a
__init__.py file which represents that this folder is a package.
Contents of setup.py
from setuptools import setup setup( name='package_abc', version='1.0', description='Example of Python package', author='NS07', packages=['package2'], )
setup() function and setuptools module.
In the above script of setup.py, you can see all the arguments are passed inside the setup() function of setuptools module. This function is used to supply most of the required information for building, distributing and installing modules. All this information is supplied to the Distutils or setuptools by the module developer and by passing the keyword arguments to setup().
Some important keyword for setup() function:
setuptools : This module comes bundled with Python programming language which deals with the python package development.
If not installed in Python you can install by this command:
pip install setuptools
So, what is setup.py?
pip uses this information to build and install this python package. setup.py also insures that the program has been written and packed using Distutils and is installed correctly. This is always at the root of the python package.
So, setup.py file plays an important role in creation and production of a python package.
Building and distribution of package
After creating setup.py script and providing all the required details to the setup() function you can build and distribute this python package to pip and others can also download and import in their program and use it.
For this run this following command:
python setup.py sdist bdist_wheel
This will create your package in the dist directory. This dist directory contains the distribution files which will be uploaded to PyPI.
bdist_wheel creates the binary distribution which holds the pre-compiled so that the package can be installed quickly.
Later you can install your package by using
pip install package_name.
So, In this python tutorial article we learned about setup.py file in python. We also learned about how this files helps in building and distributing package to pip. We also learned about setupmodules and directory structure of a python package. We also learned about setup() function of setuptools() modules and the important information it holds as the arguments inside the setup() function.
Thanks for reading.