Difference between a python module and a package

Kelvin Wangonya - Feb 20 '19 - - Dev Community

Modules

Modules are single Python files that can be imported. Any python file can be a module. For example, if I have two Python files: module.py and hello.py in the same directory:

# module.py

def hello(name):
    print("Hello {}".format(name))
Enter fullscreen mode Exit fullscreen mode

I can import that module in my hello.py:

#hello.py

import module

module.hello("World!") # Hello World!
Enter fullscreen mode Exit fullscreen mode

The same can be done in the interpreter:

>>> from module import hello
>>> hello("World!") # Hello World!
Enter fullscreen mode Exit fullscreen mode

Packages

Packages are made up of multiple Python files (or modules), and can even include libraries written in different languages like C or C++. Seeing an __init.py__ file in a folder typically tells you that that folder is a Python package. The __init__.py doesn't have to contain any code -- sometimes it does -- it just has to be there for Python take that particular folder as a package.

📁 my_package
    |- __init__.py
    |- module.py
Enter fullscreen mode Exit fullscreen mode
# __init.py__

from my_package.module import hello
Enter fullscreen mode Exit fullscreen mode

When you import my_package in your script, the __init__.py script will be run, giving you access to all of the functions in the package. In this case, it only gives access to the module.hello function.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player