Open In App

Source distribution and built distribution in python

Python is all about Distribution. It can be defined as the collection of files that together allows to build, package and distribute a module. Once a distribution exists in a system, it can easily be installed. Also, it can be shared with PyPI(Python package index) community so that other people can use your distribution. To make it more clear consider Anaconda, which is an open-source distribution of python and R programming language. It contains python, various libraries(numpy, pandas, matplotlib tech), IDE’s (like Spyder, Jupyter Notebook) and package manager conda as well as pip inside it, which enables a user to use the resources for various machine learning tasks, without downloading them explicitly. There are two types of distribution in python:

  1. Source distribution(sdist)– It contains setup.py (which contains information about module/metadata), the source files of module/script (.py files or .c/.cpp for binary modules), data files, etc. The result is an archive that can then be used to recompile everything on any platform like linux, windows, and mac.
  2. Built distribution(bdist)– It creates distribution which includes .pyc files(python bytecode), .so/.dll/.dylib for binary modules, .exe if using py2exe(extension of python, which can be used to convert scripts to exe form on windows, and can be used without installation of python) on Windows, data files… but no setup.py. The result is an archive that is specific to a platform (for example linux-x86_64) and to a version of Python. That can be installed and then used directly by extracting it into the root of your filesystem (executables are in /usr/bin (or equivalent), data files in /usr/share, modules in /usr/lib/pythonX.X/site-packages/…).

setup.py setup.py is a python file, which usually tells the system that, the module/package you are about to install has been packaged and distributed using Distutils, which is the standard for distribution of python modules. It is the most important file. It’s the file where various aspects of your project are configured. The primary feature of setup.py is that it contains a global setup() function. The keyword arguments to this function are how specific details of your project are defined. Making a basic setup.py file. 






# import setup function from
# python distribution utilities
from distutils.core import setup
 
# Calling the setup function
setup(
      name = 'nesters',
      version = '1.0.0',
      py_modules = ['addition'],
      author ='a geek',
      author_email = 'ageek@gmail.com',
      url = 'https;//ageek.com',
      description = 'a simple program to add two numbers',
      keywords='adds two numbers',
)

Let us see what different arguments of setup function do:

Let’s create an addition.py file that contains the code for the addition of two numbers. Create this file in notepad and save it as addition.py. 






def addition():
    n = 5
    print("addition of two numbers")
    while(n>= 0):
        a, b = input("enter two numbers: ").split()
        try:
            value1 = int(a)
            value2 = int(b)
            break
        except e:
            print("re enter numbers" )
    sum = value1 + value2
    print("sum of two numbers is :", sum)

Making a Source distribution:

After the execution of the following command, a dist folder which will contain a nester1-1.1.0.tar file in it and a MANIFEST FILE which contains the list of python(.py) files built by the sdist command, will be formed in the same source folder(nester). While creating a source distribution many formats can be specified using –format option. For example:

python setup.py sdist --formats=gztar,zip

Different formats are:

Format Description
zip zip file (.zip)
gztar gzip’ed tar file (.tar.gz)
bztar bzip2’ed tar file (.tar.bz2)
xztar xz’ed tar file (.tar.xz)
ztar compressed tar file (.tar.Z)
tar tar file (.tar)

Making a built distribution:

After the execution of the following command, two folders will be created :

  1. dist – which contains the “nester1-1.1.0.win-amd64” in winRarZip archive format and on extracting the zip file, an “addition.pyc” file will be found in __pycache__ folder, which is the compiled(byte code) version of your “addition.py” file, specific to the system.
  2. build – which contains library folder and “addition.py” file inside it.

While creating a built distribution many formats can be specified using –format option. For example:

python setup.py bdist --format=zip

Different formats are:

Format Description
zip zip file (.zip)
gztar compressed tar file (.tar.Z)
ztar bzip2’ed tar file (.tar.bz2)
tar tar file (.tar)
rpm RPM
pkgtool Solaris pkgtool
sdux Solaris pkgtool
wininst self-extracting ZIP file for Windows
msi Microsoft Installer

Article Tags :