Modules and Packages

Modules

A module is simply a file containing Python definitions, functions, and statements. Putting code into modules is useful because of the ability to import the module functionality into your script.

Module helps to keep everything modular and separate. For instance many modules have a read() function since this is a common thing to do. Without using the <module>.<function> (...) syntax there would be no way to know which one to call.


In [1]:
# import os module
import os
os.getcwd()


Out[1]:
'/Users/cnc/PycharmProjects/learning_python'

In [4]:
# The following command provides the details of the imported package definition
# help(os.listdir())

User Defined Module


In [7]:
# save the following code as example.py
def add(a,b):
    return a+b

# now you can import example.py
# import example
# example.add(5,4)

Import with renaming

We can import a module by renaming it as follows:


In [10]:
import math as m
print(m.pi)


3.141592653589793

from...import statement

We can import specific names from am module without importing the full module.


In [11]:
from math import pi
print(pi) # please note the dot operator is not required


3.141592653589793

To import all definitions from the module just specify '*' as below. Please note that this not a good practice as it can lead to duplicate definitions for an identifier.


In [12]:
from math import *
print(pi)


3.141592653589793

Module Search Path

While importing a module python looks for a definition at various places and in the following order.

  1. First it looks for a built-in module
  2. Looks at the current directory
  3. PYTHONPATH ( environment variable with a list of directory )
  4. Installation dependent default directory

In [13]:
import sys
sys.path


Out[13]:
['',
 '/Users/cnc/anaconda/lib/python36.zip',
 '/Users/cnc/anaconda/lib/python3.6',
 '/Users/cnc/anaconda/lib/python3.6/lib-dynload',
 '/Users/cnc/anaconda/lib/python3.6/site-packages',
 '/Users/cnc/anaconda/lib/python3.6/site-packages/Sphinx-1.5.6-py3.6.egg',
 '/Users/cnc/anaconda/lib/python3.6/site-packages/aeosa',
 '/Users/cnc/anaconda/lib/python3.6/site-packages/setuptools-27.2.0-py3.6.egg',
 '/Users/cnc/anaconda/lib/python3.6/site-packages/IPython/extensions',
 '/Users/cnc/.ipython']

Reloading a Module

Python loads modules only once even though you try to import it multiple times. But if the module is changed for some reasons and you want to reload the module you can use the .reload() function inside the 'imp' module.


In [14]:
# my_module.py
# print('This code got executed')

# import imp
# import my_module
# This code got executed
# import my_module
# import my_module
# imp.reload(my_module)

Module Functions


In [2]:
print(dir(os))


['CLD_CONTINUED', 'CLD_DUMPED', 'CLD_EXITED', 'CLD_TRAPPED', 'DirEntry', 'EX_CANTCREAT', 'EX_CONFIG', 'EX_DATAERR', 'EX_IOERR', 'EX_NOHOST', 'EX_NOINPUT', 'EX_NOPERM', 'EX_NOUSER', 'EX_OK', 'EX_OSERR', 'EX_OSFILE', 'EX_PROTOCOL', 'EX_SOFTWARE', 'EX_TEMPFAIL', 'EX_UNAVAILABLE', 'EX_USAGE', 'F_LOCK', 'F_OK', 'F_TEST', 'F_TLOCK', 'F_ULOCK', 'MutableMapping', 'NGROUPS_MAX', 'O_ACCMODE', 'O_APPEND', 'O_ASYNC', 'O_CLOEXEC', 'O_CREAT', 'O_DIRECTORY', 'O_DSYNC', 'O_EXCL', 'O_EXLOCK', 'O_NDELAY', 'O_NOCTTY', 'O_NOFOLLOW', 'O_NONBLOCK', 'O_RDONLY', 'O_RDWR', 'O_SHLOCK', 'O_SYNC', 'O_TRUNC', 'O_WRONLY', 'PRIO_PGRP', 'PRIO_PROCESS', 'PRIO_USER', 'P_ALL', 'P_NOWAIT', 'P_NOWAITO', 'P_PGID', 'P_PID', 'P_WAIT', 'PathLike', 'RTLD_GLOBAL', 'RTLD_LAZY', 'RTLD_LOCAL', 'RTLD_NODELETE', 'RTLD_NOLOAD', 'RTLD_NOW', 'R_OK', 'SCHED_FIFO', 'SCHED_OTHER', 'SCHED_RR', 'SEEK_CUR', 'SEEK_END', 'SEEK_SET', 'ST_NOSUID', 'ST_RDONLY', 'TMP_MAX', 'WCONTINUED', 'WCOREDUMP', 'WEXITED', 'WEXITSTATUS', 'WIFCONTINUED', 'WIFEXITED', 'WIFSIGNALED', 'WIFSTOPPED', 'WNOHANG', 'WNOWAIT', 'WSTOPPED', 'WSTOPSIG', 'WTERMSIG', 'WUNTRACED', 'W_OK', 'X_OK', '_Environ', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', '_execvpe', '_exists', '_exit', '_fspath', '_get_exports_list', '_putenv', '_spawnvef', '_unsetenv', '_wrap_close', 'abc', 'abort', 'access', 'altsep', 'chdir', 'chflags', 'chmod', 'chown', 'chroot', 'close', 'closerange', 'confstr', 'confstr_names', 'cpu_count', 'ctermid', 'curdir', 'defpath', 'device_encoding', 'devnull', 'dup', 'dup2', 'environ', 'environb', 'errno', 'error', 'execl', 'execle', 'execlp', 'execlpe', 'execv', 'execve', 'execvp', 'execvpe', 'extsep', 'fchdir', 'fchmod', 'fchown', 'fdopen', 'fork', 'forkpty', 'fpathconf', 'fsdecode', 'fsencode', 'fspath', 'fstat', 'fstatvfs', 'fsync', 'ftruncate', 'get_blocking', 'get_exec_path', 'get_inheritable', 'get_terminal_size', 'getcwd', 'getcwdb', 'getegid', 'getenv', 'getenvb', 'geteuid', 'getgid', 'getgrouplist', 'getgroups', 'getloadavg', 'getlogin', 'getpgid', 'getpgrp', 'getpid', 'getppid', 'getpriority', 'getsid', 'getuid', 'initgroups', 'isatty', 'kill', 'killpg', 'lchflags', 'lchmod', 'lchown', 'linesep', 'link', 'listdir', 'lockf', 'lseek', 'lstat', 'major', 'makedev', 'makedirs', 'minor', 'mkdir', 'mkfifo', 'mknod', 'name', 'nice', 'open', 'openpty', 'pardir', 'path', 'pathconf', 'pathconf_names', 'pathsep', 'pipe', 'popen', 'pread', 'putenv', 'pwrite', 'read', 'readlink', 'readv', 'remove', 'removedirs', 'rename', 'renames', 'replace', 'rmdir', 'scandir', 'sched_get_priority_max', 'sched_get_priority_min', 'sched_yield', 'sendfile', 'sep', 'set_blocking', 'set_inheritable', 'setegid', 'seteuid', 'setgid', 'setgroups', 'setpgid', 'setpgrp', 'setpriority', 'setregid', 'setreuid', 'setsid', 'setuid', 'spawnl', 'spawnle', 'spawnlp', 'spawnlpe', 'spawnv', 'spawnve', 'spawnvp', 'spawnvpe', 'st', 'stat', 'stat_float_times', 'stat_result', 'statvfs', 'statvfs_result', 'strerror', 'supports_bytes_environ', 'supports_dir_fd', 'supports_effective_ids', 'supports_fd', 'supports_follow_symlinks', 'symlink', 'sync', 'sys', 'sysconf', 'sysconf_names', 'system', 'tcgetpgrp', 'tcsetpgrp', 'terminal_size', 'times', 'times_result', 'truncate', 'ttyname', 'umask', 'uname', 'uname_result', 'unlink', 'unsetenv', 'urandom', 'utime', 'wait', 'wait3', 'wait4', 'waitpid', 'walk', 'write', 'writev']

In [20]:
import math
print(math.__doc__)
math.__name__


This module is always available.  It provides access to the
mathematical functions defined by the C standard.
Out[20]:
'math'

Packages

A package is just a way of collecting related modules together within a single tree-like hierarchy.

Like we organise the files in directories, Python has packages for directories and modules for files. Similar, as a directory can contain sub-directories and files, a python package can have sub-package and modules.

A directory must contain a file named __init.py in order for python to consider the directory as a package. This file can be empty but in general it is used for initialisation code for that package.

The following are different ways of importing the packages.

  1. import <package name>
  2. import <package name>.<module name>
  3. from <package name> import <module name>

In [25]:
# examples
import math
from math import pi
print(pi)


3.141592653589793

Further References