Swipe left or right to navigate to next or previous post

Guide on Python Packages and Modules

09 Aug 2022 . category: Python . Comments
#Python #Packaging

Ultimate guide on python packaging

This blog post is about the python, modules and packages

What is Python?

Python is one of the most popular programming language created by Guido van Rossum. It was first released on 1991.

It is object-oriented high-level interpreted and dynamic typing programming language. It has built-in dynamic data structures which supports dynamic binding. It is easy to learn.

Python is used in everything from building the production-ready web pages to system scripting, in software development to complex mathematics, from big data to perform complex mathematics. It is used in the self-controls cars to the Netflix's recommendation algorithm. Being the general purpose language, it is used in the wide range of applications that includes data science, machine learning, automation, software development, web development, automate tasks, conduct data analysis.

pros of python

  1. It has easy, simple to use and learn the syntax.
  2. It is dynamic typed language
  3. It reduces the cost of maintenance
  4. It supports the modules and packages. It encourages the code reuse and modular programs
  5. It is freely distributed and supports major platforms.

What can you do with python?

  1. Web development or software development
  2. System Scripting
  3. Data analysis and visualization
  4. Machine Learning
  5. Software Testing and prototyping

Why Python?

  1. Python works on most of the platform including Windows, Mac, Linux, Raspberry pi
  2. Simple Syntax
  3. Write programs in fewer line of codes
  4. It can be executed as soon as it is written.
  5. It can be used in any, object-oriented , functional or procedural

Why Python is so popular?

  1. It has a simple syntax which is easier to read and understand.
  2. It speeds up the project time
  3. It is open source which makes it free to use and distribute even for commercial purposes
  4. It supports modules and libraries.
  5. It has a large pools of modules and libraries contributed by the large and active community.
  6. It supports file handing and database connectivity.
  7. It comes inbuilt with the linux system like Ubuntu

Modules in Python

Module is any python file with .py extension containing the python code. Different python objects such as classes, variables, constants, functions can be defined in the module. Module organizes these objects in logical way . All the python objects are available to an interpreter session or another python module by importing the module using the import statement.

Functions defined in the modules needed to be imported before using.

Creating a Module

A module can be created by creating a text file with .py extension. A module to multiply a number can be saved as mul.py file name with following code


    def mul(a, b):
        return a*b

Different ways to define a module in python:

There are three different ways to define a module in python

  1. Creating a module in python itself
  2. A python module can be written in C and loaded dynamically
  3. A built in module contained in the interpreter

Importing a Module

A module can be imported in another python module by importing the module.

Following code can be used to import the mul file in the add.py file


    import mul
    print(mul.mul(5,2))

This will multiply the 5 and 2 and returns its result.

Module can be imported as alias using as keyword


    import mul as multiply
    print(multiply.mul(5,2))

Executing modules as scripts

A module can be executed just as it is imported. But it needs the __name__ set to __main__ which means we need to add the following code at the end of the module

    
    if __name__ == "__main__":
    import sys
    mul(int(sys.argv[1]), int(sys.argv[2]))

You can run the mul module with two params as follows in a terminal

    
  python mul.py 5 2

Packages in Python

A package contains one or more relevant modules organized in the logical way.

__init__.py

The package folder must contain the special python file called __init__.py. The __init__.py has two purposes.

  1. The folder containing the __init.py is recognized by python interpreter as a packages.
  2. It exposes the specific resources to other modules. The exposed modules can be imported in other modules or packages

The __init__.py can be normally kept empty. However, it contains import statement that import specific funtions from modules to available for import packages and modules.


    from mul import mul

The Module Search Path

This section describes what happens when we execute the following python statement

  
    python mul.py 5 2

When we execute the above statement, the python interpreter searches the mul.py in a list of directories which are assembled from the following sources.

  1. The current directory from which the script is executed
  2. The lists of directories listed on the environment variable PYTHONPATH
  3. THe installation dependent lists of directories configured at the time of Python installed./li> </ol> </section>

    The lists of directories for searching the packages in the system can be obtained from the sys module

        
        import sys
        sys.path
    


Tapan B.K. | Full Stack Software Engineer

Tapan B.K. is Full Stack Software Engineer. In his spare time, Tapan likes to watch movies, visit new places.