Startertutorials Blog
Tutorials and articles related to programming, computer science, technology and others.
Subscribe to Startertutorials.com's YouTube channel for different tutorial and lecture videos.

Categories: Python Programming. No Comments on Packages in Python Programming
0
(0)

This article provides a comprehensive overview of packages in Python programming language along with example programs.

 

Introduction

A package is a directory structure which can contain modules and sub packages. Every package in Python is a directory which must contain a special file called __init.py__. The file __init.py__ can be empty. To allow only certain modules to be imported, we can use the __all__ variable in __init.py__ file as follows:


Subscribe to our monthly newsletter. Get notified about latest articles, offers and contests.


 

__all__ = [“Stat”]

 

The above line says that only Stat module in the package can be imported.

Every package must be a folder in the system. Packages should be placed in the path which is pointed by sys.path. If the package is not available, then ImportError is raised.

 

Creating a Package

 

A package (folder) named CustomMath is created with an empty __init__.py file inside it. In the package CustomMath, a single module named BasicMath is created which contains three functions as follows:

 

def prod(x, y):
	return x*y 

def rem(x, y):
	return x%y 


def lastdigit(x):
	return x%10

 

A new Python script is written which uses our custom package named CustomMath as follows:

 

from  CustomMath.BasicMath  import  *
print(prod(4,3))
print(rem(4,3))
print(lastdigit(99))

 

PyPI and Pip

 

Third party packages are available in Python Package Index (PyPI). We can import the modules available in the third party packages by using the pip tool. Pip is a command line tool for maintaing Python packages.

 

To install a package using pip we write:

 

pip  install  package-name

 

To see the list of existing packages, we write:

 

pip list

 

Standard Library Modules

 

Python comes with several built-in modules. We can use them when necessary. These built-in modules forms the Python’s standard library. Some of the modules in standard library are:

 

  • string
  • re
  • datetime
  • math
  • random
  • os
  • multiprocessing
  • subprocess
  • email
  • json
  • doctest
  • unittest
  • pdb
  • argparse
  • socket
  • sys

 

globals(), locals(), and reload()

 

The globals() function returns all the names in the global namespace that can be accessed from inside a function.

 

The locals() function returns all the names in the local namespace i.e., within the function. Both the above functions returns the names as a dictionary.

 

The reload() function can be used to reload a module again the program if need as follows:

reload(module_name)reload(module_name)

 

How useful was this post?

Click on a star to rate it!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?

Suryateja Pericherla

Suryateja Pericherla, at present is a Research Scholar (full-time Ph.D.) in the Dept. of Computer Science & Systems Engineering at Andhra University, Visakhapatnam. Previously worked as an Associate Professor in the Dept. of CSE at Vishnu Institute of Technology, India.

He has 11+ years of teaching experience and is an individual researcher whose research interests are Cloud Computing, Internet of Things, Computer Security, Network Security and Blockchain.

He is a member of professional societies like IEEE, ACM, CSI and ISCA. He published several research papers which are indexed by SCIE, WoS, Scopus, Springer and others.

Leave a Reply

Your email address will not be published. Required fields are marked *