Open In App

Source distribution and built distribution in python

Last Updated : 09 Mar, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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. 

Python3




# 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:

  • name: It is the name of the project. The package will be listed by this name on PyPI.
  • version: It is a string which can specify the current version of the project. It is totally your choice how you want to set the scheme of the series of versions (‘1.0’ or ‘0.1’ or even ‘0.0.1’ can also be used). This version is displayed on PyPI for each release if the project is published. Every-time a new version is uploaded, you will have to change this argument as well.
  • description: A short description about the package. Long description argument can be used for long descriptions.
  • url: A homepage URL for the project. This makes it easier for people to follow or contribute to the project.
  • author, author_email: Details about the author.
  • license: specify the type of license which is used.
  • classifiers: It is a list of strings which can specify more details about the project like its development status, topic, license and supported python versions for the project.
  • install_requires: It can be used to specify what third-party libraries your package needs to run. These dependencies will be installed by pip when someone installs your package.
  • keywords: List keywords to describe the project.

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. 

Python3




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:

  • To make a source distribution, firstly make a folder and copy setup.py and addition.py scripts into that folder(let’s name it as nester). Then open terminal and change the current directory to the directory of the folder for example: if it is in the desktop – C:\Users\HP PC\Desktop\nester.
  • Lastly type setup.py sdist. This will make 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:

  • Repeat step 1 from Source distribution.
  • Lastly type setup.py bdist. This will create 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


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads