1 / 20

Python’s 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

Download Presentation

Python’s Modules

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Python’s Modules Noah Black

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

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

  4. Importing modules • Modules are imported by using built-in import command, without the .py extension >>> importexample • To make access easier, individual definitions within a module may be imported >>> fromfunctionimport func1, func2 • When modules are imported, all statements and definitions will be executed

  5. 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)

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

More Related