How to Use Python setup.py

Vaibhhav Khetarpal Feb 02, 2024
  1. Installing a Package With the Help of setup.py
  2. Creating a Package With the Help of setup.py
How to Use Python setup.py

Like other popular commands such as pip or easy_install, setup.py is utilized to install the different available packages in Python. It is mostly utilized in cases where the packages need to be installed in Python manually.

In this article, we will learn about setup.py and how to use it.

Installing a Package With the Help of setup.py

In simpler words, setup.py is a script contained within the packages or libraries to ensure their full and correct installation. The setup.py script is written and uses Python programming language.

If the package that we extracted into a directory contains a setup.py file, we can directly install that package by running the following command.

python setup.py install

Creating a Package With the Help of setup.py

Further, we move on to how to create our own setup.py file. The setup.py command is a part of the setuptools library that needs to be installed in Python before using this command.

The packages that cannot be installed in other ways, like using the pip command, are usually installed using setup.py. The package needs to be extracted to any local directory and worked upon further using setup.py to implement this successfully.

To clearly explain, we are using the general Python slang foo in the following code. Let us suppose that our directory contains the following structure.

foo
├── foo
│   ├── data_struct.py
│   ├── __init__.py
│   └── internals.py
├── README
├── requirements.txt
└── setup.py

The setup.py python script that we will create would look like this:

from setuptools import setup

setup(
    name="foo",
    version="2.0",
    description="Something different",
    author="Jinku Hu",
    author_email="foomail@foo.com",
    packages=["foo"],  # would be the same as name
    install_requires=[
        "wheel",
        "bar",
        "greek",
    ],  # external packages acting as dependencies
)

The setup.py file is now ready and can be utilized to install your newly created package to any device using Python with the help of the simple pip command.

Vaibhhav Khetarpal avatar Vaibhhav Khetarpal avatar

Vaibhhav is an IT professional who has a strong-hold in Python programming and various projects under his belt. He has an eagerness to discover new things and is a quick learner.

LinkedIn