Python package

Python package is organization of code directory. that can be contains various modules and configuration files. in simple word that is an directory structures of codes file. this directory is much contains of __init__.py file. look at this example.

python directory example

In this structure App is main mackage and (Setting,Operation,Db) is sub module.

App Db Operation Settings __init__.py __init__.py delete.py insert.py update.py __init__.py close.py open.py __init__.py close.py open.py

In this example we can access any module of this package outside of App folder. suppose we are use the module of App>>Db>>delete.py file. first see the delete.py.

#file delete.py
def deleteData():
  print("Delete Data Function")

This file are contain one function deleteData(). so how to use this file function outside the app folder?. that is very simple. help of package directory we can access this file function.

Package.Subpakage.file.function

for example suppose test.py is exist outside app folder. and this file imported package of (App>>Dd>>delete) module.

#test.py
import App.Db.delete
App.Db.delete.deleteData()
Output
Delete Data Function

__init__.py is used to access module of directory. so it is very important to define this file. this file may be empty but that are used to create package.

for example if we are delete the __init__.py file in App folder (app root __init__.py) then python3 is display an error. see this example.

 python3 test.py
Traceback (most recent call last):
  File "test.py", line 1, in <module>
    import App.Db.delete
ImportError: bad magic number in 'App': b'\x03\xf3\r\n'


Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved