What is setup.py?

shittu_olumide_

Shittu Olumide

Posted on April 19, 2022

What is setup.py?

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 
Enter fullscreen mode Exit fullscreen mode

OR

$ pip install 
Enter fullscreen mode Exit fullscreen mode

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:

  1. 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.
  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 specifying the name of the package.
  2. version: A string specifying the version number of the package.
  3. description: A string describing the package in a single line.
  4. author: A string specifying the author of the package.
  5. long_description: A string providing a longer description of the package.
  6. 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.
  7. url: A string specifying the URL for the package homepage(usually the GitHub repository or the PyPI page).
  8. download_url: A string specifying the URL to download the package.
  9. package_data: This is a dictionary with package names as keys and lists of glob patterns as values.
  10. py_modules: A list of strings specifying the modules that setuptools will manipulate.
  11. python_requires: This is a comma-separated string containing Python version specifiers for the package's supported Python versions.
  12. install_requires: A string list containing the bare minimum of dependencies necessary for the package to operate correctly.
  13. keywords:A list of strings or a comma-separated string providing descriptive meta-data.
  14. 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.
  15. 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'],
     )

Enter fullscreen mode Exit fullscreen mode

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',
        ]
    }
)

Enter fullscreen mode Exit fullscreen mode

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.

  1. 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.
  2. 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.

Feel free to reach me on Twitter and Hashnode

💖 💪 🙅 🚩
shittu_olumide_
Shittu Olumide

Posted on April 19, 2022

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related