Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Module vs. Package?

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?

like image 474
SpaceCadet Avatar asked Nov 09 '17 02:11

SpaceCadet


2 Answers

x may be a package or a module and y is something inside that module/package.

A module is a .py file, a package is a folder with an __init__.py file. When you import a package as a module, the contents of __init__.py module are imported.

like image 183
geckos Avatar answered Sep 30 '22 12:09

geckos


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
like image 25
SpaceCadet Avatar answered Sep 30 '22 13:09

SpaceCadet