Python Packages Tutorial – How to Create Your Own Package
Master Python with 70+ Hands-on Projects and Get Job-ready - Learn Python
In today’s article, we will discuss Python Packages. Moreover, we will learn the structure of Python Packages and how to import modules from packages in Python.
So, let’s start the Python Packages Tutorial.
Python Packages Tutorial – How to Create Your Own Package in Python
What are Python Packages?
In our computer systems, we store our files in organized hierarchies. We don’t store them all in one location. Likewise, when our programs grow, we divide it into packages.
In real-life projects, programs are much larger than what we deal with in our journey of learning Python. A package lets us hold similar modules in one place.
Like a directory may contain subdirectories and files, a package may contain sub-packages and modules. We have been using modules a lot in the previous lessons.
Remember math, os, and collections? Those were all modules that ship with Python officially. We will discuss the difference between a module and a package in our next lesson.
But for now, let’s dive into the world of Python packages.
Structure of Python Packages
Technology is evolving rapidly!
Stay updated with DataFlair on WhatsApp!!
As we discussed, a package may hold other Python packages and modules. But what distinguishes a package from a regular directory? Well, a Python package must have an __init__.py file in the directory.
You may leave it empty, or you may store initialization code in it. But if your directory does not have an __init__.py file, it isn’t a package; it is just a directory with a bunch of Python scripts. Leaving __init__.py empty is indeed good practice.
Take a look at the following structure for a game:
Here, the root package is Game. It has sub packages Sound, Image, and Level, and file __init__.py. Sound further has modules load, play, and pause, apart from file __init__.py.
Image has modules open, change, and close, apart from __init__.py. Finally, Level has modules start, load, and over, apart from __init__.py.
How to Import Modules from Packages in Python?
A Python package may contain several modules. To import one of these into your program, you must use the dot operator(.)
In the above example, if you want to import the load module from subpackage sound, we type the following at the top of our Python file:
import Game.Sound.load
Note that we don’t type the extension, because that isn’t what we refer to the module as. The subpackage Level has a module named load too, but there is no clash here.
This is because we refer to the module by its fully qualified name.
To escape having to type so much every time we needed to use the module, we could also import it under an alias:
import Game.Sound.load as loadgame
(If you’re working the interpreter, you may also do the following:
loadgame=Game.Sound.load
This works equally fine.)
Alternatively, you could do:
from Game.Sound import load
Now, if the Sound subpackage has a function volume_up(), we call it this way:
loadgame.volume_up(7)
If we imported this way:
from Game.Sound.load import volume_up() as volup
We could call the function simply, without needing to use a full qualifier:
volup(7)
But this isn’t recommended, as this may cause names in a namespace to clash.
Further Notes
When you import a package, only the modules directly under it are imported. An import does not import the sub packages.
>>> import one >>> one.two
Output
Also note that if you want to check where your Python packages are being created, your path will look something like this:
C:\Users\lifei\AppData\Local\Programs\Python\Python36-32\Lib\site-packages
How to Create Your Own Python Package?
Now, on to the most interesting part. Like we said, Python packages are nothing but a dictionary with sub-packages and modules, and an __init__.py file.
In our example, this is the hierarchy we create:
This is what we have in evenodd.py:
def check(): a=int(input('Enter a number')) if a%2==0: print("Even") else: print("Odd")
Also, we keep each __init__.py empty.
Now, we import and use it this way:
>>> from one.two.evenodd import check as check >>> check()
Output
Odd
>>> check()
Output
Even
So, this was all about Python Packages. Hope you like our explanation.
Python Interview Questions on Packages
- What are the Python Packages?
- How many packages are there in Python?
- What is Python Dist Packages?
- How to create a Python package?
- Why do we need packages in Python?
Conclusion
In this Python Packages tutorial, we discussed packages, and how to create them. Apart from that, the Python Package Index(PyPI) provides us with a lot of Python packages to help us with our projects.
Did you like our efforts? If Yes, please give DataFlair 5 Stars on Google
Well done guys. This is excellent. Your explanations have more clarity than I have ever come across for any examples I have ever had on Python Packages. Thank you very much.
NB: Do you have a similar on on Java or JavaScript?
Hello Collen,
We are glad that you liked our article. Check this DataFlair’s Java Packages article. This might help you.
Its good place to start with python. Contents like editing __init__.py file, can also be included to wrap the topic. Its seems to like having open ends.
We are glad that you like the Python Packages Tutorial. Stay with DataFlair for more learning..
Thanks for your work! It’s easy to understand. But I want to post a suggestion, I think these words
“Now if the Sound subpackage has a function volume_up(), we call it this way:loadgame.volume_up(7)” should be modified by
“Now if the load module has a function volume_up(), we call it this way:loadgame.volume_up(7)
How do I import a python file from another subpackage from within another subpackage
/project
/packages
/subpack1
__init__.py
file1.py
/subpack2
__init__.py
file2.py
How can I import file1 from inside of file2 in python
Here’s how you can import file1 from inside of file2 in python
from ..subpackage1 import file1
/project
/packages
/subpack1
__init__.py
file1.py
/subpack2
__init__.py
file2.py
How can I import file1 from inside of file2 in python
Here’s how you can import file1 from inside of file2 in python
from ..subpackage1 import file1
from .packages.subpack1 import file1
Sir How to install ‘_pywrap_tenserflow’ package