Python Programming/Modules

This lesson introduces Python modules and packages.

Objectives and Skills
Objectives and skills for this lesson include:
 * Modules
 * Byte-compiled .pyc files
 * The from ..import ..statement
 * A module's __name__ and custom modules
 * The dir function and packages

Readings

 * 1)  Modular programming
 * 2) Python.org: Modules

Multimedia

 * 1) YouTube: Python Modules

Modules
Modules are regular Python .py files that define functions or classes that may be imported and reused in other Python programs.

functions.py:

classes.py:

The import Statement
The import statement finds a module, loads it, initializes it if necessary, and defines a name or names which are used to reference code in the module.

Output: In function1 In method1

The from ... import Statement
The from...import statement finds a module, loads it, initializes it if necessary, and then adds module references to the local namespace, allowing functions and classes to be accessed without a module reference. Use of import rather than from...import is preferred. While from ... import supports an * option to import all references rather than naming specific functions or classes, this use is discouraged.

Output: In function1 In method1

Module Name
When modules are imported, the __name__ variable is set to the name of the module. When the Python interpreter runs a module directly, the __name__ variable is set to "__main__". This allows a module designed to be imported to add a main function that will only execute when the module is run directly.

The dir Function
The dir function returns the list of names in the current local scope, or for the object, if specified.

Tutorials

 * 1) Complete one or more of the following tutorials:
 * 2) * LearnPython
 * 3) ** Modules and Packages
 * 4) * TutorialsPoint
 * 5) ** Modules
 * 6) * Wikibooks
 * 7) ** A Beginner's Python Tutorial/Importing Modules

Practice

 * 1) Create a module with at least one function or class. In a separate module, import the first module and use the dir function to display the names in the local scope. Then import the first module using from ... import * and use the dir function to display the names in the local scope. Compare the difference between import and from ... import.
 * 2) Modify one or more of the Python Programming/Functions activities so that the function code is in a different module. Use import to import the module and then call the function.
 * 3) Modify one or more of the Python Programming/Classes activities so that the class code is in a different module. Use import to import the module and then instantiate and use the class.
 * 4) Review Python.org: os. Import the Python os library and then use getlogin and uname to display the current username, computer name, operating system, and version.

Lesson Summary

 * Modular programming is a software design technique that emphasizes separating the functionality of a program into independent, interchangeable modules, such that each contains everything necessary to execute only one aspect of the desired functionality.
 * Modules are regular Python .py files that define functions or classes that may be imported and reused in other Python programs.
 * The import statement finds a module, loads it, initializes it if necessary, and defines a name or names which are used to reference code in the module.
 * The from...import statement finds a module, loads it, initializes it if necessary, and then adds module references to the local namespace, allowing functions and classes to be accessed without a module reference.
 * Use of import rather than from...import is preferred.
 * While from ... import supports an * option to import all references rather than naming specific functions or classes, this use is discouraged.
 * When modules are imported, the __name__ variable is set to the name of the module. When the Python interpreter runs a module directly, the __name__ variable is set to "__main__".
 * __name__ allows a module designed to be imported to add a main function that will only execute when the module is run directly.
 * To speed up loading modules, Python caches the compiled bytecode version of each module.
 * A program doesn’t run any faster when it is read from a .pyc file than when it is read from a .py file; the only thing that’s faster about .pyc files is the speed with which they are loaded.
 * Python comes with a library of standard modules.
 * The sys module provides access to operating system or operating environment variables and functions.
 * The dir function returns the list of names in the current local scope, or for the object, if specified.

Key Terms

 * bytecode
 * A form of instruction set designed for efficient execution by a software interpreter.

Assessments

 * Flashcards: Quizlet: Python Modules
 * Quiz: Quizlet: Python Modules