Trusted answers to developer questions
Trusted Answers to Developer Questions

RELATED TAGS

What is setup.py?

Shittu Olumide Ayodeji

Overview

This shot will discuss an important question: What is setup.py , and why is it essential in Python programming?

Every programming language has a distinct and unique method for installing packages from the source. setup.py can help developers manage the dependencies of their packages in a way that allows them to redistribute them easily.

This shot will talk about the primary usage of setup.py, the keywords used in the setup.py file, how to write the first setup.py file, and deploying your package to the public.

The setup.py file is a Python file which indicates that the installation module/package is 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. The setup.py is a Python script typically included with Python-written libraries or apps. Its objective is to ensure that the program is installed correctly.

This also allows to install python packages with ease using:

python setup.py install 

or

$ pip install 

With the aid of pip, we can use the setup.py to install any module without having to call setup.py directly. The setup.py 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.

Use of Setup.py

The setup.py file may be the most significant file that should be placed at the root of the Python project directory. It primarily serves two purposes:

  1. It includes choices and metadata about the program, such as the package name, version, author, license, minimal dependencies, entry points, data files, and so on.
  2. 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:

  1. name: A string containing the package’s name.
  2. version: A string containing the package’s version number.
  3. description: A single-line text explaining the package.
  4. author: A string identifying the package’s creator/author.
  5. long_description: A string containing a more detailed description of the package.
  6. maintainer: It's a string providing the current maintainer’s name, if not the author. If the maintainer is not given, the author in PKG-INFO will be utilized by the setup tools.
  7. url: A string providing the package’s homepage URL (usually the GitHub repository or the PyPI page).
  8. download_url: A string containing the URL where the package may be downloaded.
  9. package_data: This is a dictionary where the keys are package names and the values are lists of glob patterns.
  10. py_modules: A string list containing the modules that setup tools will modify.
  11. python_requires: This is a comma-separated string providing Python version specifiers for the package’s supported Python versions.
  12. install_requires: A string list containing only the dependencies necessary for the package to function effectively.
  13. keywords: A comma-separated string or string list providing descriptive meta-data.
  14. entry_points: This is a dictionary with keys corresponding to entry point names and values corresponding to the actual entry points stated in the source code.
  15. license: A string containing the package’s licensing information.

You can find a comprehensive list of the keywords here, in the official documentation.

How to write the 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, it may include 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 the setup.py file

Finally, we're ready to post the package to PyPi.org so that others may use pip install yourpackage to install it. Using these two procedures, we may deploy the setup.py file.

  1. Step 1: First, we publish it on the temporary test.pypi.org server. Next, we publish it on the permanent pypi.org server for the public to utilize the package.
  2. Step 2: We follow this step when we've the login credentials and are familiar with the technique. We may publish right immediately on the permanent pypi.org server (e.g., username, password, package name).

Conclusion

We've discussed how to use setup.py file in Python to handle package dependencies and ease package distribution. We also discussed the basic purpose of setup.py, the keywords used in setup.py files, creating the first setup.py file, and publishing the package for public use.

Please note that the Python’s package distribution requirements are always developing, most notably in the last few years, so make sure to stay up to speed and follow best practices on a regular basis.

RELATED TAGS

CONTRIBUTOR

Shittu Olumide Ayodeji
RELATED COURSES

View all Courses

Keep Exploring

Learn in-demand tech skills in half the time

Copyright ©2022 Educative, Inc. All rights reserved.

soc2