Advance Python Modules – How to Create & Import with dir Function

Free Python courses with 57 real-time projects - Learn Python

In this Python Modules tutorial, we will discuss what is a module in Python programming language? Moreover, we will talk about how to create python modules and import modules in python.

Along with this, we will learn how can we execute python module as a script, standard Python modules, and Python dir functions.

So, let’s start Python Modules Tutorial.

Python Modules

Python Modules

What is Python Module?

Python module is but a piece of 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 modules looks like this:

calc.py

How to Create Python Modules?

Let’s create a Python modules ‘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).

How can we Import Modules in Python?

Now, to import Python modules, 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__

Output

‘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)

Output

1.0
>>> fd(9,4)

Output

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

Output

<class ‘int’>
>>> type(fd(5.5,4))

Output

<class ‘float’>

More on Python Modules and Importing

Python modules 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 Python. 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 modules:

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

Output

2.25
>>> fd(9,4)

Output

2

We can also import all from a module:

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

Output

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

Output

<function sub at 0x0655CC90>

How to Execute 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.

Python Module Search Path

Whenever we import Python modules, 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.

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.

Python Standard 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

Output

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

Python dir() Function

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

>>> dir(sys)

Output

[‘__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)

Output

__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()

Output

[‘__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)

Output

[‘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.

Python Interview Questions on Modules

  1. What is module in Python? Explain with example.
  2. Explain the purpose for using Python modules.
  3. How many modules are there in Python?
  4. How to install and open a Python module?
  5. Where are Python modules stored?

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.

Your 15 seconds will encourage us to work even harder
Please share your happy experience on Google

follow dataflair on YouTube

8 Responses

  1. Susmitha says:

    Could you please provide some more information on “How to use python modules as a script”?

    • DataFlair Team says:

      Hi Susmitha

      Thanks for the comment on Python Modules. In the example in the text above, the statement if __name__ == ‘__main__’: checks whether the file is being executed as a script or is in use as a module by another script.

      Before the Python interpreter executes the code in a source file, it defines some special variables. If the file runs as the main program, then you have a variable __name__ which has the value of ‘main’. If, however, it is imported into a ‘main’ that is already running, this will set the variable __name__ to the module’s name in its scope.

      This is why it works.
      You can refer our side bar, there are more articles on Python Modules, which also help you!
      Keep learning and keep sharing
      DataFlair

  2. Ravikumar says:

    Hi, Could you please explain if __name__==__main__: and what is the use of this?

    • DataFlair says:

      __name__ is a special variable which is defined by python interpretor before executing the source code.if __name__ == “main”: is used to execute the code only if the file was run directly by the user, not when it is imported.

  3. Ravikumar says:

    How to add the code(script) into a calc.py file?

    • DataFlair says:

      You can create a calc.py file using any editor (you can even use notepad) and then you can type/copy the code in that file

  4. Aaftab says:

    with the help of any code editor, u can do this.
    just open it and save the code as the name u want .py

Leave a Reply

Your email address will not be published. Required fields are marked *