What is setup.py in a Python Project?

So you created a python package or module or an application and want to share it to other people so that they can use it or install it into their existing app. For this purpose, you need a solution to manage packaging, versioning, and other metadata of your package. 

Or even if you want to share your package on the Python Package Index (PyPI), you need to manage metadata, packaging and distribution on your package and that is where setup.py comes into play. 



A typical setup.py contains following information:

Package Metadata: setup.py contains metadata about your project, such as its name, version, author, and description. This metadata is used to identify and describe your package.

Package Dependencies: You can specify the dependencies that your project relies on, either directly within the setup.py file or by including a separate requirements.txt file that lists dependencies.

Scripts and Entry Points: If your project includes command-line scripts, you can specify them using the scripts or entry_points arguments. This allows you to create command-line tools that can be installed alongside your package.

Package Structure: You define which directories and files should be included in your package by specifying the packages and package_dir arguments.

Long Description: You can include a long description of your project, often in the form of a README file or a detailed documentation link.

Classifiers: Classifiers are metadata tags that categorize your project. These tags help users find your package on PyPI.


Now let's have a look at a sample setup.py script:

from setuptools import setup, find_packages

setup(
name="my-package",
version="1.0.0",
author="Your Name",
description="A short description of your project",
packages=find_packages(),
install_requires=[
"dependency1",
"dependency2",
],
)


To use setup.py, you typically run it with the setuptools package's setup() function. This script allows you to build, package, and distribute your Python project. It's also crucial for creating distributable packages that can be easily installed using tools like pip. You can create a source distribution or a binary distribution (e.g., wheels) of your project using this script.


For example, to create a source distribution package, you can run:

python setup.py sdist

Or to install your package locally you can run below pip command

pip install .


There can be a detailed post on setup.py on how this works in case of packaging python application. For example if your project has non python files like config.ini or config.cfg, then how to include those in the package or If your project has data file data.dat then how to avoid it from being included in packaged zip file. 

I will try to explain these pints below:

Using setup.py for various packing and distribution tasks: 

---




Apart from setuptools there are some other packages that are based on setuptools and makes it easy to manage your complex projects and activities like uploading distributable package to PyPI. Here is a list of such tools:

  1. twine: Twine is a popular Python package for securely uploading Python distributions (e.g., source distributions or wheel binary distributions) to the Python Package Index (PyPI) or other compatible package indexes. It's commonly used by Python developers to publish their packages and libraries for others to install using tools like pip.
  2. flit: It simplifies the setuptools process using simple commands like flit build and flit publish etc. 
  3. poetry: It is also an extension of setuptools and simplifies the process of package management. 






Post a Comment

Previous Post Next Post