Organizing code: more on importing modules

Modules in Python

Modules in Python

Overview

Modules allows to split a long program into several files
for instance - to put function/class definitions in one or more files, and the main program to be written in a separate file.

Python modules

Module == Python file
Each module creates its own namespace.
A module can contain definitions, as well as executable statements

Creating a custom Python module

Its just simple as creating a Python file - do not forget the .py extension


      ├── my_app
      │   ├── app.py           # the main file
      │   ├── helper_module.py # a module file
    

Import module: import X

To use a module, you have to import it first.
As a module generates its own namespace, you have to prefix the imported names with the module name:

        # import the helper_module:
        import helper_module

        # use helper_module
        user_name = helper_module.get_user_name()
        helper_module.greet(user_name)
      

Module alias: import X as Y

You can give modules names a shorter aliases:

      import helper_module as hm

      user_name = hm.get_user_name()
      hm.greet(user_name)
    

Import object from module: from X import Y

You can import an object from module, using the
from X import Y notation. After that, you can use the name (Y), without having to prefixing it.
You can import more that one object, if you separate them with comma

      from helper_module import get_user_name, greet

      user_name = get_user_name()
      greet(user_name)
    

Import object from module: from X import *

Imports all objects from a module.
Not recommended to use it, as a name collision could occurs.

      from helper_module import *

      user_name = get_user_name()
      greet(user_name)
    

Where import looks for a module?

The directory containing the input script
the directories specified in the PYTHONPATH environment variable
The installation-dependent default.

Relative import

A relative import uses the relative path (starting from the path of the current module) to the desired module to import

Relative import - example


      $ tree my_app
      my_app/
      ├── app.py
      ├── lib
      │   ├── helper_module.py
    

      import lib.helper_module as hm

      user_name = hm.get_user_name()
      hm.greet(user_name)
    

Python's built-in modules

or why Python is "Batteries Included"
Python Module Index
to use a built-in (standard) module, you have to import it!

Python's third-party modules

The Python Packaging Index PyPIis a public repository of open source licensed packages made available for use by other Python users.
Installing Python Modules @python3 docs
Creating Virtual Environments
Managing Application Dependencies

Packages in Python

Packages in Python

Overview

A Python package is simply a group of Python module(s), usually stored in one directory
Packages are a way of structuring Python’s module namespace by using “dotted module names”.

make directory a package: __init__.py

You should put an __init__.py file in the directory, which you want it to be treated as package from Python.
__init__.py can be an empty file
or you can put some initialization code for the package

a package - example


  		$ tree my_app
  		my_app/
  		├── app.py
  		└── packA
  		    ├── greet.py
  		    ├── __init__.py
  		    └── packB
  		        ├── get_data.py
  		        └── __init__.py
  	

Importing packages

You can import a package using the same mechanism as importing a module. But do not forget - a package makes its namespace, as well

Import-related module attributes

Import-related module attributes

Overview

Python modules are objects and as such they have attributes.
Most useful module attributes are:
__name__
__file__

__name__

When Python interpreter reads a .py file, it executes the code in it!
If a .py file is executed as a module:
__name__ is set to module's own filename
If a .py file is executed as stand-alone program:
__name__ is set to "__main__"

__name__ - examples

Create in same directory next Python files:


      import helper_module
    

      if __name__ == "__main__":
        print("helper_module is executed as stand-alone py file")
      else:
        print("helper_module is imported as module")
    
Run the main.py and look at the output
Run the helper_module.py and look at the output

__file__

Stores the full filename (including path) of the file/module being executed
Will be same, no matter if the file is executed as a stand-alone or as a module.
If we need to strip the path, and get just the filename,

__file__ - examples

Create in same directory next Python files:


      import helper_module
    

      print( "__file__:", __file__)
    
Run the main.py and look at the output
Run the helper_module.py and look at the output

These slides are based on

customised version of

Hakimel's reveal.js

framework