Create and access Python package

Python Methods and Functions

Packages — it is a way of structuring many packages and modules, which helps in a well-organized hierarchy of the dataset by making it easier to access catalogs and modules. Just as there are different drives and folders in the OS that help us store files, packages also help us store other subpackages and modules so that they can be used by the user as needed.

Build and Explore Packages

To tell Python that a particular directory is a package, we create a file inside it called __init__.py, and then it is treated as a package and we can create others in it. modules and subpackages. This __init__.py file can be left blank or encoded using the package initialization code. 
To create a package in Python, we need to follow three simple steps:

  1. First, we create a directory and give it a package name, preferably related to its operation.
  2. Then we put the classes and necessary functions in it.
  3. Finally, we create an __init__.py file inside the directory so that Python knows that the directory is a package.
  4. An example of creating a package

    Let's take a look at this example and see how a package is created. Let's create a package named Cars and create three modules in it, namely Bmw, Audi and Nissan.

    1. First we create a directory and name it Cars.
    2. Then we need to create modules . To do this, we need to create a file called Bmw.py and create its content by placing this code in it.

      # Python code to illustrate modules

      class Bmw:

        # First, we create a constructor for this class

      # and add members to it, here are the models

      def __ init __ ( self ):

        self . models = [ 'i8' , 'x1' , ' x5' , 'x6' ]

        

      # Normal print function

        def outModels ( self ):

      print ( ' These are the available models for BMW' )

      for model in self . models:

      print ( '% s' % model)

      Then we create another file named Audi.py and add a similar type of code with different members to it.

      # Python code to illustrate the module

      class Audi:

      # First, we create a constructor for this class

      # and add members to him, here are the models

      def __ init__ ( self ):

      self . models = [ 'q7' , ' a6' , ' a8' , 'a3' ]

       

      # Normal print function

      def outModels ( self ):

      print ( 'These are the available models for Audi' )

      for  model in self . models:

      print ( '% s' % model)

      Then we create another file called Nissan.py and add a similar type of code with different members to it.

      # Python code to illustrate the module

      class Nissan:

      # First, we create a constructor for this class

      # and add members to it, here are the models

        def __ init __ ( self ):

      self .models = [ ' altima' , '370z' , ' cube ' , ' rogue' ]

       

      # Normal print function

      def outModels ( self ):

      print ( ' These are the available models for Nissan' )

      for model in self . models:

      print ( '% s' % model)

    3. Finally, we create the __init__.py file. This file will be placed in the Cars directory and can be left empty, or we can put this initialization code in it.

      from Bmw import Bmw < p> from Audi import Audi

      from Nissan import Nissan

    4. Now let's use package we created. To do this, create a sample.py file in the same directory as the Cars package and add the following code to it:

      # Import classes from your new packaging

      from Cars import Bmw

      from Cars import Audi

      from Cars import Nissan

       
      # Create a Bmw class object and call its method

      ModBMW = Bmw ()

      ModBMW.outModels ()

       
      # Create an Audi class object and call its method

      ModAudi = Audi ()

      ModAudi.outModels ()

       
      # Create a Nissan class object and call its method

      ModNissan = Nissan ()

      ModNissan.outModels ()

      Different ways to access packages

      Let's take a look at this example and try to link with it packages and how we can access it. 

      1. import in batches
        Suppose that cars and brand catalogs are packages. For them to be a package, they must all contain an __init__.py file, either empty or with some initialization code. Suppose all car models are supposed to be modules. Using packages helps to import any modules, individually or in whole. 
        Let's say we want to get a Bmw i8. The syntax for this would be:
         'import' Cars.Bmw.x5 

        When importing a package or subpackages or modules, Python traverses the entire directory tree looking for a specific package and acts systematically as programmed by the dot operator. 
        If any module contains a function and we want to import it. For example, a8 has a get_buy (1) function and we want to import that, the syntax would be:

         import Cars.Audi.a8 Cars.Audi.a8.get_buy (1) 

        When using only import syntax, remember that the last attribute must be a subpackage or module, it must not be the name of any function or class.

      2. "from ... import" in packages
        Now, when we need to use such a function, we need to write the entire long line after importing the parent package. To get through this easier, we use the "from" keyword. To do this, we first need to inject a module using & # 39; from & # 39; and & # 39; import & # 39 ;:
         from Cars.Audi import a8 

        Now we can call the function anywhere using

         a8.get_buy (1) 

        There is another method, which is less time consuming. We can directly import the function and use it where needed. First import it using:

         from Cars.Audi.a8 import get_buy 

        Now call the function from anywhere:

         get_buy (1) 
      3. "from ... import *" in packages
        Using the syntax from ... import , we can import anything from submodules into a class, function or a variable defined in the same module. If the mentioned attribute in the import part is not defined in the package, then the compiler throws an ImportError exception. 
        Importing submodules can cause unwanted side effects that occur when importing submodules explicitly. Thus, we can import different modules at the same time using the *. Syntax:
         from Cars.Chevrolet import * 

        This imports everything, i.e. modules, submodules, functions, classes, from a subpackage.

      This article contributed by Chinma Lenka . If you are as Python.Engineering and would like to contribute, you can also write an article using contribute.python.engineering or by posting an article contribute @ python.engineering. See my article appearing on the Python.Engineering homepage and help other geeks.

      Please post comments if you find anything wrong or if you'd like to share more information on the topic discussed above.





Tutorials