Python Modules


1. Python Modules

What is a module in Python? How do we import it? How do we execute it? All these questions will be answered in this blog Post on Python Modules. So lets begin with the introduction to Python Modules.

Python Modules

Python Modules

2. An Introduction to Python Modules

A module is but a piece of Python code.

Exiting the interpreter destroys all functions and variables we created. But when we want a longer program, we create a script. With Python, we can put such definitions in a file, and use them in a script, or in an interactive instance of the interpreter. Such a file is a module. If you face any difficulty in article on Python modules, ask us in comments.

In essence, a module is a file that contains Python statements and definitions. A Python module looks like this:

calc.py

3. Creating a Python Module

Let’s create a Python module ‘calc’.

Microsoft Windows [Version 10.0.16299.309]

(c) 2017 Microsoft Corporation. All rights reserved.

 

C:\Users\lifei>cd Desktop

 

C:\Users\lifei\Desktop>mkdir calc

 

C:\Users\lifei\Desktop>cd calc

 

C:\Users\lifei\Desktop\calc>echo >__init__.py

 

C:\Users\lifei\Desktop\calc>echo >calc.py

 

C:\Users\lifei\Desktop\calc>

And this is what we put inside the module calc.py:

def add(a,b):

return a+b

def sub(a,b):

return a-b

def mul(a,b):

return a*b

def div(a,b):

return a/b

def exp(a,b):

return a**b

def floordiv(a,b):

return a//b

Also, calc is a package we create, and we place __init__.py inside it (Refer to Python Packages).

4. Importing Modules in Python

Now, to import this module in Python, we first get to the Desktop in Python.

>>> import os
>>> os.chdir('C:\\Users\\lifei\\Desktop\\calc')
>>> import calc
>>>

To find the name of this module, we can use the __name__ attribute.

>>> calc.__name__

‘calc’

We can now use functions from this module:

We can also assign one of these functions a name:

>>> fd=calc.floordiv
>>> fd(5.5,4)

1.0

>>> fd(9,4)

2

>>> type(fd(9,4))

<class ‘int’>

>>> type(fd(5.5,4))

<class ‘float’>

Read: Python Module vs Package

5. More on Python Modules and Importing

A Python module may contain anything from executable statements to function definitions. Such statements initialize the module. Hence, they execute only once, when we import the module. However, they also run if we execute the file as a script.

Each module uses its own private symbol table globally for all of its functions. So, its author can use global variables in the module without worrying that they will accidentally clash with a user’s global variables.

A module can always import other modules. In fact, we can place import statements at the beginning of a module/script, but we don’t ‘need’ to. This places the imported module’s name in the importing module’s symbol table.

We can also selectively import functions from a Python module:

>>> from calc import div as d,floordiv as fd
>>> d(9,4)

2.25

>>> fd(9,4)

2

We can also import all from a module:

>>> from calc import *
>>> floordiv(9,4)

2

This will import all names other than those beginning with an underscore(_). However, we disparage this use, as it makes for poorly readable code.

We can also import a module under an alias.

>>> import calc as cal
>>> cal.sub

<function sub at 0x0655CC90>

6. Executing Python Modules as Scripts

Look at the following code:

def add(a,b):

print(a+b)

def sub(a,b):

print(a-b)

def mul(a,b):

print(a*b)

def div(a,b):

print(a/b)

def exp(a,b):
    print(a**b)
def floordiv(a,b):
    print(a//b)

if __name__ == “__main__”:

import sys

 

if int(sys.argv[1])==1:
        add(int(sys.argv[2]),int(sys.argv[3]))
    elif int(sys.argv[1])==2:
        sub(int(sys.argv[2]),int(sys.argv[3]))

These last few lines let us use the sys module to deal with command line arguments. To execute subtraction, this is what we type in the command prompt:

C:\Users\lifei\Desktop\calc>python calc.py 2 3 4

-1

This way, you can complete the code for other operations as well. Hence, we’ve created a script. But we can also import this normally like a module:

>>> import calc
>>>

We may want to run a module as a script for testing purposes.

Any Doubt yet in Python Modules? Please Comment.

7. The Module Search Path

Whenever we import a module, say eggs, the interpreter searches a built-in version. If not found, it searches for a file named eggs.py under a list of directories given by variable sys.path. This variable is initialized from the following locations:

  • The directory holding the input script (or the current directory, in case no file is specified).
  • PYTHONPATH (a list of directory names, with the same syntax as the shell variable PATH).
  • The installation-dependent default.

Once initialized, a Python program may modify sys.path.

8. Compiled Python Files

In an attempt to speed up loading a module, Python will cache each module’s compiled version in the __pycache__ directory. It does so under the name module.version.pyc. Here, the version encodes the compiled file’s format. For instance, under CPython 3.3, we’ll have eggs.py as __pycache__/eggs.cpython-33.pyc. This allows compiled modules from different Python versions and releases to coexist. These compiled modules are platform-independent.

If the compiled version is out of date, Python recompiles it automatically.

9. Standard Python Modules

And like we’ve always said, Python ships with a library of standard Python modules. While some of them are built into the interpreter, we can create our own. The standard ones lend us extra functionality, in turn reducing the need to code too much. Other times, they provide efficiency to a programmer, in cases like providing access to operating system primitives, the likes of system calls.

The module sys is built into every Python interpreter. However, some modules are only available to certain operating platforms. For instance, the winreg module is only available to Windows programmers.

The sys module will also tell you which version of Python you are using.

>>> import sys
>>> sys.version

‘3.6.0 (v3.6.0:41df79263a11, Dec 23 2016, 07:18:10) [MSC v.1900 32 bit (Intel)]’

10. The dir() Function

The dir() is a built-in function that returns a sorted list of all the names that a Python module defines.

>>> dir(sys)

[‘__displayhook__’, ‘__doc__’, ‘__excepthook__’, ‘__interactivehook__’, ‘__loader__’, ‘__name__’, ‘__package__’, ‘__spec__’, ‘__stderr__’, ‘__stdin__’, ‘__stdout__’, ‘_clear_type_cache’, ‘_current_frames’, ‘_debugmallocstats’, ‘_enablelegacywindowsfsencoding’, ‘_getframe’, ‘_home’, ‘_mercurial’, ‘_xoptions’, ‘api_version’, ‘argv’, ‘base_exec_prefix’, ‘base_prefix’, ‘builtin_module_names’, ‘byteorder’, ‘call_tracing’, ‘callstats’, ‘copyright’, ‘displayhook’, ‘dllhandle’, ‘dont_write_bytecode’, ‘exc_info’, ‘excepthook’, ‘exec_prefix’, ‘executable’, ‘exit’, ‘flags’, ‘float_info’, ‘float_repr_style’, ‘get_asyncgen_hooks’, ‘get_coroutine_wrapper’, ‘getallocatedblocks’, ‘getcheckinterval’, ‘getdefaultencoding’, ‘getfilesystemencodeerrors’, ‘getfilesystemencoding’, ‘getprofile’, ‘getrecursionlimit’, ‘getrefcount’, ‘getsizeof’, ‘getswitchinterval’, ‘gettrace’, ‘getwindowsversion’, ‘hash_info’, ‘hexversion’, ‘implementation’, ‘int_info’, ‘intern’, ‘is_finalizing’, ‘last_traceback’, ‘last_type’, ‘last_value’, ‘maxsize’, ‘maxunicode’, ‘meta_path’, ‘modules’, ‘path’, ‘path_hooks’, ‘path_importer_cache’, ‘platform’, ‘prefix’, ‘set_asyncgen_hooks’, ‘set_coroutine_wrapper’, ‘setcheckinterval’, ‘setprofile’, ‘setrecursionlimit’, ‘setswitchinterval’, ‘settrace’, ‘stderr’, ‘stdin’, ‘stdout’, ‘thread_info’, ‘version’, ‘version_info’, ‘warnoptions’, ‘winver’]

>>> for i in dir(calc): print(i)

__builtins__

__cached__

__doc__

__file__

__loader__

__name__

__package__

__spec__

add

div

exp

floordiv

mul

sub

And without any arguments, dir() returns a lilst of the names that we have defined currently.

>>> dir()

[‘__annotations__’, ‘__builtins__’, ‘__doc__’, ‘__loader__’, ‘__name__’, ‘__package__’, ‘__spec__’, ‘add’, ‘cal’, ‘calc’, ‘div’, ‘exp’, ‘floordiv’, ‘i’, ‘mul’, ‘os’, ‘sub’, ‘sys’]

To get a list of built-in functions and variables, we do the following, instead:

>>> import builtins
>>> dir(builtins)

[‘ArithmeticError’, ‘AssertionError’, ‘AttributeError’, ‘BaseException’, ‘BlockingIOError’, ‘BrokenPipeError’, ‘BufferError’, ‘BytesWarning’, ‘ChildProcessError’, ‘ConnectionAbortedError’, ‘ConnectionError’, ‘ConnectionRefusedError’, ‘ConnectionResetError’, ‘DeprecationWarning’, ‘EOFError’, ‘Ellipsis’, ‘EnvironmentError’, ‘Exception’, ‘False’, ‘FileExistsError’, ‘FileNotFoundError’, ‘FloatingPointError’, ‘FutureWarning’, ‘GeneratorExit’, ‘IOError’, ‘ImportError’, ‘ImportWarning’, ‘IndentationError’, ‘IndexError’, ‘InterruptedError’, ‘IsADirectoryError’, ‘KeyError’, ‘KeyboardInterrupt’, ‘LookupError’, ‘MemoryError’, ‘ModuleNotFoundError’, ‘NameError’, ‘None’, ‘NotADirectoryError’, ‘NotImplemented’, ‘NotImplementedError’, ‘OSError’, ‘OverflowError’, ‘PendingDeprecationWarning’, ‘PermissionError’, ‘ProcessLookupError’, ‘RecursionError’, ‘ReferenceError’, ‘ResourceWarning’, ‘RuntimeError’, ‘RuntimeWarning’, ‘StopAsyncIteration’, ‘StopIteration’, ‘SyntaxError’, ‘SyntaxWarning’, ‘SystemError’, ‘SystemExit’, ‘TabError’, ‘TimeoutError’, ‘True’, ‘TypeError’, ‘UnboundLocalError’, ‘UnicodeDecodeError’, ‘UnicodeEncodeError’, ‘UnicodeError’, ‘UnicodeTranslateError’, ‘UnicodeWarning’, ‘UserWarning’, ‘ValueError’, ‘Warning’, ‘WindowsError’, ‘ZeroDivisionError’, ‘_’, ‘__build_class__’, ‘__debug__’, ‘__doc__’, ‘__import__’, ‘__loader__’, ‘__name__’, ‘__package__’, ‘__spec__’, ‘abs’, ‘all’, ‘any’, ‘ascii’, ‘bin’, ‘bool’, ‘bytearray’, ‘bytes’, ‘callable’, ‘chr’, ‘classmethod’, ‘compile’, ‘complex’, ‘copyright’, ‘credits’, ‘delattr’, ‘dict’, ‘dir’, ‘divmod’, ‘enumerate’, ‘eval’, ‘exec’, ‘exit’, ‘filter’, ‘float’, ‘format’, ‘frozenset’, ‘getattr’, ‘globals’, ‘hasattr’, ‘hash’, ‘help’, ‘hex’, ‘id’, ‘input’, ‘int’, ‘isinstance’, ‘issubclass’, ‘iter’, ‘len’, ‘license’, ‘list’, ‘locals’, ‘map’, ‘max’, ‘memoryview’, ‘min’, ‘next’, ‘object’, ‘oct’, ‘open’, ‘ord’, ‘pow’, ‘print’, ‘property’, ‘quit’, ‘range’, ‘repr’, ‘reversed’, ’round’, ‘set’, ‘setattr’, ‘slice’, ‘sorted’, ‘staticmethod’, ‘str’, ‘sum’, ‘super’, ‘tuple’, ‘type’, ‘vars’, ‘zip’]

This was all on Python modules. Hope the Python Modules article was informative.

11. Conclusion

While this is all about Python modules, we suggest you should also read about Packages in Python. Then, maybe you should switch to Packages vs Modules. For any question regarding Python Modules, Please Comment. Until then, have a good day.