# 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

* Wikipedia: Modular programming
* Python.org: Modules

## Multimedia

* YouTube: Python Modules

## Examples
### 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:

### 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:

### 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.

## Activities
### Tutorials

* Complete one or more of the following tutorials:
** LearnPython
*** Modules and Packages
** TutorialsPoint
*** Modules
** Wikibooks
*** Wikibooks: A Beginner's Python Tutorial/Importing Modules

### Practice

* 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.
* Modify one or more of the Python Programming/Functions#Practice activities so that the function code is in a different module. Use import to import the module and then call the function.
* Modify one or more of the Python Programming/Classes#Practice activities so that the class code is in a different module. Use import to import the module and then instantiate and use the class.
* 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.

## Review Questions

* Modular programming is _____. 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.
* Python modules are _____. Python modules are regular Python .py files that define functions or classes that may be imported and reused in other Python programs.
* 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.
* 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.
* When importing Python modules, use of _____ rather than _____ is preferred. When importing Python modules, se of import rather than from...import is preferred.
* While from ... import supports _____, this use is discouraged. 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 _____. When the Python interpreter runs a module directly, the __name__ variable is set to _____. 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 _____. __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 _____. 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 _____ file than when it is read from a _____ file; the only thing that’s faster about _____ files is _____. 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 _____. Python comes with a library of standard modules.
* The sys module _____. The sys module provides access to operating system or operating environment variables and functions.
* The dir function _____. The dir function returns the list of names in the current local scope, or for the object, if specified.

## Assessments

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

## See Also

* Python.org: Modules
* Python.org: The import system
* SoloLearn: Python
* Open Book Project: Python 3 - Modules
* Real Python: Python Modules and Packages