
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Packages in Python
A package is a hierarchical file directory structure that defines a single Python application environment that consists of modules and subpackages and sub-subpackages, and so on.
Consider a file Pots.py available in Phone directory. This file has following line of source code −
#!/usr/bin/python def Pots(): print "I'm Pots Phone"
Similar way, we have another two files having different functions with the same name as above −
- Phone/Isdn.py file having function Isdn()
- Phone/G3.py file having function G3()
Now, create one more file __init__.py in Phone directory −
- Phone/__init__.py
To make all of your functions available when you've imported Phone, you need to put explicit import statements in __init__.py as follows −
from Pots import Pots from Isdn import Isdn from G3 import G3
After you add these lines to __init__.py, you have all of these classes available when you import the Phone package.
#!/usr/bin/python # Now import your Phone Package. import Phone Phone.Pots() Phone.Isdn() Phone.G3()
When the above code is executed, it produces the following result −
I'm Pots Phone I'm 3G Phone I'm ISDN Phone
In the above example, we have taken example of a single functions in each file, but you can keep multiple functions in your files. You can also define different Python classes in those files and then you can create your packages out of those classes.
- Related Articles
- What are the packages in Python?
- Add packages to Anaconda environment in Python
- How to create python namespace packages in Python 3?
- How do I Install Python Packages in Anaconda?
- Capacity To Ship Packages Within D Days in Python
- What are various sub-packages in Python SciPy library?
- Packages in Java
- Packages in C#
- How to develop programs with Python Namespaced Packages?
- How to organize Python classes in modules and/or packages
- Explain packages in Java
- What are Some Good Python Packages for Machine Learning?
- What are packages in Java?
- Types of packages in Java
- Overview of Packages in Oracle
