Python – Packages

What is Package: Python modules may contain several classes, functions, variables, etc. whereas Python packages contain several modules. In simpler terms, Package in Python is a folder that contains various modules as files.

Python has a vast ecosystem of third-party packages and libraries that extend its functionality for various purposes.

Here are some commonly used Python packages across different domains:

We organize a large number of files in different folders and subfolders based on some criteria, so that we can
find and manage them easily. In the same way, a package in Python takes the concept of the modular approach to
next logical level. As you know, a module can contain multiple objects, such as classes, functions, etc.

A package can contain one or more relevant modules. Physically, a package is actually a folder containing one or more module files.

Let’s create a package named mypackage, using the following steps:

Create a new folder named D:\MyApp.
Inside MyApp, create a subfolder with the name ‘mypackage’.
Create an empty init.py file in the mypackage folder.
Using a Python-aware editor like IDLE, create modules greet.py and functions.py with the following code:

def SayHello(name):
print(“Hello “, name)
def sum(x,y):
return x+y

def average(x,y):
return (x+y)/2

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

That’s it. We have created our package called mypackage. The following is a folder structure:

Install a Package Globally:

Once a package is created, it can be installed for system-wide use by running the setup script. The script calls setup() function from the setuptools module.

Let’s install mypackage for system-wide use by running a setup script.

Save the following code as setup.py in the parent folder MyApp. The script calls the setup() function from the setuptools module. The setup() function takes various arguments such as name, version, author, list of dependencies, etc. The zip_safe argument defines whether the package is installed in compressed mode or regular mode.

Example:

from setuptools import setup
setup(name=’mypackage’,
version=’0.1′,
description=’Testing installation of Package’,
url=’#’,
author=’auth’,
author_email=’author@email.com’,
license=’MIT’,
packages=[‘mypackage’],
zip_safe=False)

Python training in hyderabad

contact

Enquiry Now
close slider
Scroll to Top
Call Now Button