Creating Python packages

Creating Python packages Tutorial: The Creating Python packages are a way of structuring packages and modules which helps in a well-organized hierarchy of data set. It makes the directories and modules easy to access like different drives and folders in an OS to help us store files. Packages will help us in storing sub-packages and modules used by the user when necessary. We will store all files in the computer at the same location and use a well-organized hierarchy of directories for accessing purpose. The program grows large size with a lot of modules when placed in the same module.
They are placed in one package and different modules in different packages. They contain subdirectories and files as a python package as a sub-package and modules. They contain a file named _init_.py as a package and left empty.

Creating the package files

We create files to package and prepare it for distribution also creates the new files as,
Packaging_tutorial/
Example_pkg/
_init_.py
Tests/
Setup.py
LICENCE
README.md

Creating a test folder

The tests/ is a placeholder for unit files and leave it empty.
We create setup.py as it is build script and tell about your package as well as which code files to include.
We open setup.py and then enter the following content.
Also, update the package name to include the username that ensures you have a unique package name.

Import:-
We import the modules from packages using the .dot operator.

Example:-

Import game.level.start
The module will contain a function as select_difficulty ().
Game.level.start.select_difficulty (2)
Start.select_difficulty (2).
We create a package using the following steps-

  1. We create a new folder named as the D:\MyApp.
  2. Then create a subfolder with the name 'mypackage'.
  3. Create an empty __init__.py file in mypackage folder.
  4. The editor is like IDLE that creates modules greet.py and functions.py.

Example:-

  greet.py
  def SayHello (name):
  print("Hello” + name)
  return
  functions.py
  def sum(x, y):
  return x+y
  def average(x, y):
  return (x+y)/2
  def power(x, y):
  return x**y
We create a package called mypackage. pp>python
We then import the function module from the mypackage package and call its power () function.
We can import specific functions from a module in the package__init__.py.
The package contains a special file called __init__.py that stores the package's content.
The interpreter recognizes a folder as the package if it contains an__init__.py file.
The __init__.py will expose specific resources from modules to be imported.
An empty __init__.py file will make all functions from modules available when this package is imported.
We will create test.py in the MyApp folder to test mypackage as,
test.py
from mypackage import power, average, SayHello
SayHello ()
x=power (3, 2)
print ("power (3, 2): ", x)

Install a Package:-

After creating the package it can be installed for system-wide use by running the setup script.
Here the script will call setup () function from the setuptools module.
We save the code as setup.py in the parent folder 'MyApp' and the script calls the setup () function from the setuptools module.
The setup () function will take the various arguments as name, version, author.
The zip_safe argument will define the package is it installed in compressed mode or regular mode.
Example:-
setup.py
from setuptools import setup
setup (name='mypackage',
version='0.1',
description='Testing installation of Package',
url='#',
author='malhar',
author_email='mlathkar@gmail.com',
license='MIT',
packages= ['mypackage'],
zip_safe=False)

Creating and Exploring Packages

We create a directory that gives package names, related to its operation and then we put the classes.
Then create an __init__.py file inside the directory to know the directory is a package.
Example of Creating Package:-
Here we create a directory and name it Cars then create modules. To do this we then we need to create a file with the name Bmw.py.
  Create its content by code,
Example:-
 class Bmw:
 def __init__ (self):
 self.models = ['i8', 'x1', 'x5', 'x6']
 def outModels (self):
 print(‘these are the available models for BMW')
 for model in self.models:
            print('\t%s ' % model)
After this we will create another file with the name Audi.py and add the same type of code with different members.

Example:-

             class Audi:
             def __init__ (self):
             self.models = ['q7', 'a6', 'a8', 'a3']
             def outModels (self):
             print('These are the available models for Audi')
             for model in self.models:
             print('\t%s ' % model)
The next file is created with the name Nissan.py and adds the similar type of code to it with different members.
Example:-
                 class Nissan:
                 def __init__ (self):
                 self.models = ['altima', '370z', 'cube', 'rogue'] 
                 def outModels (self):
                 print('These are the available models for Nissan')
                 for model in self.models:
          Print ('\t%s ' % model)
Lastly we will create the __init__.py file and this file will be placed inside Cars directory.
from Bmw import Bmw
from Audi import Audi
from Nissan import Nissan

We use the package created and to do this we make a sample.py file in the same directory.

Example:-


from Cars import Bmw
from Cars import Audi
from Cars import Nissan
ModBMW = Bmw ()
ModBMW.outModels ()
ModAudi = Audi ()
ModAudi.outModels ()
ModNissan = Nissan ()
ModNissan.outModels ()

Additional Services : Refurbished Laptops Sales, Python Classes, Share Market Classes And SEO Freelancer in Pune, India