Python Modules vs Packages | Differences Between Python Modules and Packages
1. Python Modules vs Packages
In our article on Python Modules vs Packages, we discussed what they are, and saw how to create our own. We also saw where they are located in our file system. But throughout the article, we saw a lot of similarities to modules. Hence, we dedicate this article to Differences Between Python Modules and Packages.
2. What is Python Modules?
A module is a Python file containing Python statements and definitions. For example, a file evenodd.py is a module, and we call it ‘evenodd’. We put similar code together in one module. This helps us modularize our code, and make it much easier to deal with. And not only that, a module grants us reusability. With a module, we don’t need to write the same code again for a new project that we take up.
In our previous article, we created a module evenodd in package two. This is what evenodd.py holds:
def check(): a=int(input('Enter a number')) if a%2==0: print("Even") else: print("Odd")
a. How to Import a Python Module?
So, as you can see, a module simply contains Python code. Consequently, we can import it, like a package.
>>> import one.two.evenodd >>>
To call function check(), we do the following:
>>> from one.two.evenodd import check >>> check()
Enter a number7
Another example would be the constants ‘pi’ and ‘e’ from the ‘math’ module.
>>> import math >>> from math import pi >>> math.pi
We can also import using an alias or using the from..import statement, like we do with packages. To import everything from a module, we do the following:
>>> from math import * >>> e
Let’s update evenodd.py to have two functions- check and evenodd.
def check(): a=int(input('Enter a number')) if a%2==0: print("Even") else: print("Odd") def add(a,b): return a+b
Now, if we want to import all functions from module evenodd, we can just use the wildcard *:
>>> from one.two.evenodd import * >>> check()
Enter a number0
b. Search Path
When we import a module, the interpreter first looks in the current directory. Then, it looks into PYTHONPATH, an environment variable with a list of directories. Finally, it looks into the installation-dependent default directory.
>>> import sys >>> for i in sys.path: print(i)
c. Reloading a Python Module
A module is imported only once. This is for efficiency purposes. Let’s add this code to evenodd.py:
print("Loading evenodd") num=7
Now, let’s restart the shell, and import evenodd thrice.
>>> import one.two.evenodd
>>> import one.two.evenodd >>> import one.two.evenodd
See? It imported it only once. Because of this, Python gives us a function to let us reload the module when we want to. This is in the module ‘imp’.
>>> import imp >>> imp.reload(one.two.evenodd)
<module ‘one.two.evenodd’ from ‘C:\\Users\\lifei\\AppData\\Local\\Programs\\Python\\Python36-32\\lib\\site-packages\\one\\two\\evenodd.py’>
Finally, dir() will let us check the components of a module.
>>> dir(one.two.evenodd)[‘__builtins__’, ‘__cached__’, ‘__doc__’, ‘__file__’, ‘__loader__’, ‘__name__’, ‘__package__’, ‘__spec__’, ‘add’, ‘check’ , ‘num’] We saw this function when we talked about the Built-in Functions in Python.
3. What is Python Packages?
A package, in essence, is like a directory holding subpackages and modules. While we can create our own packages, we can also use one from the Python Package Index (PyPI) to use for our projects.
To import a package, we type the following:
We can also import it giving it an alias:
import Game.Sound.load as load game
You can’t import a function using the dot operator(.) For that, you must type this:
from Game.Sound.load import volume_up
A package must have the file __init__.py, even if you leave it empty.
But when we import a package, only its immediate modules are imported, not the sub-packages. If you try to access those, it will raise an AttributeError.
To get a deeper insight into packages, check Python Packages.
4. Differences Between Python Modules and Packages
So, now that we’ve revised both modules and packages, let’s see how they differ:
- A module is a file containing Python code. A package, however, is like a directory that holds sub-packages and modules.
- A package must hold the file __init__.py. This does not apply to modules.
- To import everything from a module, we use the wildcard *. But this does not work with packages.
This was all about the article on Python Modules vs Packages
Now that we know what subtle differences exist between a module and a package, let’s take up a challenge and take up a project of our own. Let’s do this to test our package skills? Okay.
Furthermore, if you have any query, feel free to approach us!