Open In App

What is setup.py in Python?

Last Updated : 09 Dec, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Introduction

In Python, setup.py is a module used to build and distribute Python packages. It typically contains information about the package, such as its name, version, and dependencies, as well as instructions for building and installing the package. This information is used by the pip tool, which is a package manager for Python that allows users to install and manage Python packages from the command line. By running the setup.py file with the pip tool, you can build and distribute your Python package so that others can use it.

Example

Following is an example of a setup.py file for a simple Python package called my_package. 

First, create a folder named ‘my_package’ with a python file named ‘__init__.py’ inside it. This ‘__init__.py’ file will be the root of the my_package and will contain all the functions and classes in the my_package module. Now your directory structure should look something like this:

Resulting directory structure

Now, add the following contents to the setup.py file:

Python3




from setuptools import setup
  
setup(
    name='my_package',
    version='0.1',
    description='A sample Python package',
    author='John Doe',
    author_email='jdoe@example.com',
    packages=['my_package'],
    install_requires=[
        'numpy',
        'pandas',
    ],
)


Here, the setup function is called with several arguments that provide information about the package. The name and version arguments specify the package name and version, while the description and author arguments provide a brief description of the package and the author’s name. The author_email argument specifies the author’s email address, and the packages argument indicates which packages should be included in the distribution. Finally, the install_requires argument specifies the package dependencies, which are other packages that must be installed in order for the my_package package to work properly.

How to use setup.py?

To use the setup.py file in Python, you first need to have the setuptools module installed. You can do this by running the following command:

Python3




pip install setuptools


Once you have setuptools installed, you can use the setup.py file to build and distribute your Python package by running the following command:

Python3




python setup.py sdist bdist_wheel


This command will create a distribution of your package in the dist directory. The sdist option creates a source distribution, which is a package that contains the source code for your package. The bdist_wheel option creates a binary distribution, which is a package that contains pre-compiled versions of your package’s modules and can be installed more quickly than a source distribution.

To install your package, you can use the pip tool. For example, to install the my_package package from the example above, you would run the following command:

Python3




pip install my_package


This will install the my_package package and any of its dependencies that are not already installed on your system. Once the package is installed, you can use it in your Python programs by importing it like any other module. For example:

Python3




import my_package
  
# Use the functions and classes in the my_package module


Overall, the setup.py file is an important part of building and distributing Python packages, as it contains the necessary information for building and installing your package. By using the pip tool and the setup.py file, you can easily share your Python packages with others and make them available for use in their own projects.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads