Question & Answers

How does a Python module differ from a python package?

Asked by sharifah Abdulkareem, telecommunications engineer at 2:30 pm


Whenever I do from 'x' import 'y' I was wondering which one is considered the
'module' and which is the 'package', and why it isn't the other way around?



I, for one, am thankful for this question, because the answer is concise and gives the exact needed knowledge.
The documentation is all fine and dandy, but it's verbose and contains way more information
than what the OP was asking for, and certainly more than I needed. I just wanted an answer to that specific question,
and the answer below is exactly what I wanted. Many of us just don't need such formal or in-depth answers.

commented by Ssesanga Abdul karim, Developer at Twitter at 3:33pm

A Python module is simply a Python source file, which can expose classes, functions and global variables.
When imported from another Python source file, the file name is treated as a namespace.
A Python package is simply a directory of Python module(s).
For example, imagine the following directory tree in /usr/lib/python/site-packages:
mypackage/__init__.py <-- this is what tells Python to treat this directory as a package
mypackage/mymodule.py
So then you would do:
import mypackage.mymodule
or
from mypackage.mymodule import myclass

Answered by katerega Lawrence, Junior developer at facebook at 8:30am