Every day, a new set of questions arises that must be addressed. What setup.py is and why it is important in Python programming is a question that has been bothering me recently. Every programming language has a distinct and unique method for installing packages from the source. Eventually, setup.py can help developers manage the dependencies of their packages in a way that allows them to easily redistribute them.
In this article, we will talk about setup.py, the primary usage of setup.py, the keywords used in setup.py file, writing your first setup.py file, and deploying your package to the public.
What is Setup.py
The setup.py file is a python file, the presence of which indicates that the module/package you are going to install was most likely packed and distributed using Distutils, the Python Module distribution standard. It specifies the contents of a module to Distutils so that it may execute the appropriate actions (such as locating dependencies) during the module's first installation. setup.py
is a Python script that is typically included with Python-written libraries or apps. Its objective is to ensure that the program is properly installed.
This also allows you to install python packages with ease using:
python setup.py install
OR
$ pip install
With the aid of pip, you can use the setup.py to install any module, without having to call setup.py directly. setup.py
It is a standard Python file. It can have any name, but by convention, it is named setup.py
so that each script does not have a separate method.
Primary purpose of Setup.py.
The setup.py file is most likely the most significant file that should be placed at the root of your Python project directory, and it primarily serves two purposes:
- It includes choices and metadata about your program, such as the package name, version, author, license, minimal dependencies, entry points, data files, and so on.
- Secondly, it serves as the command line interface via which packaging commands may be executed.
Keywords in setup.py.
Examples of some keywords used when calling the setup()
method:
name
: A string specifying the name of the package.version
: A string specifying the version number of the package.description
: A string describing the package in a single line.author
: A string specifying the author of the package.long_description
: A string providing a longer description of the package.maintainer
: A string specifying the name of the current maintainer, if different from the author. Note that if the maintainer is provided, setup tools will use as the author in PKG-INFO.url
: A string specifying the URL for the package homepage(usually the GitHub repository or the PyPI page).download_url
: A string specifying the URL to download the package.package_data
: This is a dictionary with package names as keys and lists of glob patterns as values.py_modules
: A list of strings specifying the modules that setuptools will manipulate.python_requires
: This is a comma-separated string containing Python version specifiers for the package's supported Python versions.install_requires
: A string list containing the bare minimum of dependencies necessary for the package to operate correctly.keywords
:A list of strings or a comma-separated string providing descriptive meta-data.entry_points
: This is a dictionary in which the keys correspond to the names of entry points and the values to the actual entry points described in the source code.license
: A string specifying the license of the package.
You can find a comprehensive list of the keywords here in the official documentation.
Writing your first setup.py script.
The setup file may include only a few lines of code.
from distutils.core import setup
setup(name='Distutils',
version='1.0',
description='Python Distribution Utilities',
author='Greg Ward',
author_email='gward@python.net',
url='https://www.python.org/sigs/distutils-sig/',
packages=['distutils', 'distutils.command'],
)
Alternatively, more sophisticated lines of code with more information.
from setuptools import setup
setup(
name='app-name'
version='1.0',
author='Bejamin Frakline',
description='A brief synopsis of the project',
long_description='A much longer explanation of the project and helpful resources',
url='https://github.com/BenjaminFranline',
keywords='development, setup, setuptools',
python_requires='>=3.7, <4',
packages=find_packages(include=['exampleproject', 'exampleproject.*']),
install_requires=[
'PyYAML',
'pandas==0.23.3',
'numpy>=1.14.5',
'matplotlib>=2.2.0,,
'jupyter'
],
package_data={
'sample': ['sample_data.csv'],
},
entry_points={
'runners': [
'sample=sample:main',
]
}
)
Deploying your setup.py file
Finally, you're ready to post your package to PyPi.org so that others may use pip install yourpackage
to install it. Using these two procedures, you may deploy your setup.py file.
- Step 1: To familiarize yourself with the technique, publish it on the temporary test.pypi.org server, and then publish it on the permanent pypi.org server for the public to utilize your package.
- Step 2: If you are already familiar with the technique and have your login credentials, you may publish right immediately on the permanent pypi.org server (e.g., username, password, package name)
Conclusion
We explored how to use setup.py file in Python to handle package dependencies and ease package distribution in this article. We also discussed the basic purpose of setup.py, the keywords used in setup.py files, creating your first setup.py file, and publishing your package to the world. Always, bear in mind that Python's package distribution requirements are always developing notably in the last few years, so make sure to stay up to speed and follow best practices on a regular basis.
Thanks for reading, feel free to drop your comments below.