Pythons Modules Noah Black Contents What are modules

Python’s Modules Noah Black

Contents • • • What are modules? Importing modules Accessing module components Executing modules as scripts Module search path Compiled Python files Standard modules The dir() function Packages Importing * from a package Intra-package references Packages in multiple directories

What are modules? • Definitions of functions and variables are not saved when interpreter is exited • Modules allow definitions to be saved for later access • Modules also allow statements to be run as executable scripts • Modules are files with. py extension

Importing modules • Modules are imported by using built-in import command, without the. py extension >>> import example • To make access easier, individual definitions within a module may be imported >>> from function import func 1, func 2 • When modules are imported, all statements and definitions will be executed

Accessing module components • To use functions defined in module, type module name followed by dot >>> example. func(3) • If function in module was imported individually with from, the module name and the dot may be excluded >>> func(3)

Executing modules as scripts • Python scripts with. py extension maybe executed in command line with python example. py <arguments> • Imported modules and executed modules can be distinguished by accessing global variable __name__: the value of __name__ is “__main__” only when modules are executed.

Modules as scripts, cont’d • Control statements can be used as follows: if __name__ == "__main__": • All code contained in if statement will not run if module is imported

Module search path • Interpreter looks for all imported modules in certain designated places, in the following order, until the module is found: 1. Current directory 2. The list of directories in PYTHONPATH environment variable 3. Installation-dependent default path

Module search path, cont’d • Environment variable PYTHONPATH is a list of directories • Current directory, PYTHONPATH, and installation-dependent default path are all stored in sys. path variable

Compiled Python files • Compiled version of example. py is stored in example. pyc • If no. pyc file exists when a module is imported, it is created • Upon future imports, the already compiled version is used to save time, unless original. py file has a new modification time

Standard modules • There is a standard library of Python modules • These modules contain built-in operations that are not actually a part of Python at its core • Some modules are only imported if a certain operating system is being used • One important library module is sys

The sys module • Two variable contained in sys, ps 1 and ps 2, hold the values of the strings in the primary and secondary prompts. These strings can be changed to your preference. >>> import sys >>> sys. ps 1 = 'plurt> ' plurt> print ‘plurt' plurt>

The dir() function • The dir() function returns a list of names (variables and functions) that are defined by the module passed to it. >>> dir(sqrt) [‘num', ‘result'] • When dir is executed with no arguments, it returns a list of all currently defined names, except those that are built-in

Packages • Packages are organized collections of modules, modules within modules • Modules are stored in directories, with each directory containing a __init__. py file that tells the interpreter that it is a package directory • Each subdirectory of root package directory is considered a subpackage

Packages, cont’d • The package or any of its subpackages may then be imported. import currency. conversion. euro • Any of euro’s definitions or subpackages can now be accessed, but only using dots: euro. convertfromdollar(10)

Importing * from a package • Sometimes you may want to import everything inside a package or subpackage. In this case you may use this command: from example. subexample import * • This loads all module names contained in variable __all__, which is itself contained in the package directory’s __init__. py file.

Importing * from a package, cont’d • If __all__ variable is not defined, then by default, importing * from a package will import all defined names of the package, and the package itself. Any submodules will not be imported unless they are explicitly imported by __init__. py or by

Intra-package references • Any module in a package may import any other modules in the same directory by name without using any dots • For importing any modules in a sibling folder, a module may use an absolute import

Packages in multiple directories • The location in which a package searches for its __init__. py may be changed, by modifying the variable __path__, which stores the value of the directory the __init__. py is in

Sources • http: //docs. python. org/library/ – Information on Python’s standard library • http: //docs. python. org/tutorial/modules. html – Tutorial on Python’s modules
- Slides: 20