Tuesday, 22 March 2016

Packages in Python with example

Packages in Python with example:

A package is a hierarchical file directory structure that defines a single Python application environment that consists of modules and sub packages and sub-sub packages, and so on.

Consider a file Test.py available in TestVal directory.

I am writing the below code in that file
def Test():
   print "I'm Test TestVal"
Similar way, we have another two files having different functions with the same name as above
·        TestVal/Isdn.py file having function Isdn()
·        TestVal/FUN.py file having function FUN()

The __init__.py in python:

Now, create one more file __init__.py in TestVal directory
To make all of your functions available when you've imported TestVal, you need to put explicit import statements in __init__.py as follows −

from Test import Test
from Isdn import Isdn
from FUN import FUN
After you add these lines to __init__.py, you have all of these classes available when you import the TestVal package.
# Now import your TestVal Package.
import TestVal
The result of the above code is

The above code shows the single function example, we can also write multiple functions in the same file. You can also define different Python classes in those files and then you can create your packages out of those classes.

No comments: