Python’s Modules
-
Upload
amelia-shelton -
Category
Documents
-
view
25 -
download
0
description
Transcript of Python’s Modules
![Page 1: Python’s Modules](https://reader035.fdocuments.us/reader035/viewer/2022080902/56812f69550346895d94f3cb/html5/thumbnails/1.jpg)
Python’s Modules
Noah Black
![Page 2: Python’s Modules](https://reader035.fdocuments.us/reader035/viewer/2022080902/56812f69550346895d94f3cb/html5/thumbnails/2.jpg)
• 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
Contents
![Page 3: Python’s Modules](https://reader035.fdocuments.us/reader035/viewer/2022080902/56812f69550346895d94f3cb/html5/thumbnails/3.jpg)
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
![Page 4: Python’s Modules](https://reader035.fdocuments.us/reader035/viewer/2022080902/56812f69550346895d94f3cb/html5/thumbnails/4.jpg)
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 func1, func2
• When modules are imported, all statements and definitions will be executed
![Page 5: Python’s Modules](https://reader035.fdocuments.us/reader035/viewer/2022080902/56812f69550346895d94f3cb/html5/thumbnails/5.jpg)
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)
![Page 6: Python’s Modules](https://reader035.fdocuments.us/reader035/viewer/2022080902/56812f69550346895d94f3cb/html5/thumbnails/6.jpg)
Executing modules as scripts
• Python scripts with .py extension maybe executed in command line withpython 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.
![Page 7: Python’s Modules](https://reader035.fdocuments.us/reader035/viewer/2022080902/56812f69550346895d94f3cb/html5/thumbnails/7.jpg)
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
![Page 8: Python’s Modules](https://reader035.fdocuments.us/reader035/viewer/2022080902/56812f69550346895d94f3cb/html5/thumbnails/8.jpg)
Module search path
• Interpreter looks for all imported modules in certain designated places, in the following order, until the module is found:1. Current directory2. The list of directories in PYTHONPATH
environment variable3. Installation-dependent default path
![Page 9: Python’s Modules](https://reader035.fdocuments.us/reader035/viewer/2022080902/56812f69550346895d94f3cb/html5/thumbnails/9.jpg)
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
![Page 10: Python’s Modules](https://reader035.fdocuments.us/reader035/viewer/2022080902/56812f69550346895d94f3cb/html5/thumbnails/10.jpg)
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
![Page 11: Python’s Modules](https://reader035.fdocuments.us/reader035/viewer/2022080902/56812f69550346895d94f3cb/html5/thumbnails/11.jpg)
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
![Page 12: Python’s Modules](https://reader035.fdocuments.us/reader035/viewer/2022080902/56812f69550346895d94f3cb/html5/thumbnails/12.jpg)
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.
>>> import sys >>> sys.ps1 = 'plurt> ' plurt> print ‘plurt' plurt plurt>
![Page 13: Python’s Modules](https://reader035.fdocuments.us/reader035/viewer/2022080902/56812f69550346895d94f3cb/html5/thumbnails/13.jpg)
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
![Page 14: Python’s Modules](https://reader035.fdocuments.us/reader035/viewer/2022080902/56812f69550346895d94f3cb/html5/thumbnails/14.jpg)
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
![Page 15: Python’s Modules](https://reader035.fdocuments.us/reader035/viewer/2022080902/56812f69550346895d94f3cb/html5/thumbnails/15.jpg)
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)
![Page 16: Python’s Modules](https://reader035.fdocuments.us/reader035/viewer/2022080902/56812f69550346895d94f3cb/html5/thumbnails/16.jpg)
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.
![Page 17: Python’s Modules](https://reader035.fdocuments.us/reader035/viewer/2022080902/56812f69550346895d94f3cb/html5/thumbnails/17.jpg)
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
![Page 18: Python’s Modules](https://reader035.fdocuments.us/reader035/viewer/2022080902/56812f69550346895d94f3cb/html5/thumbnails/18.jpg)
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
![Page 19: Python’s Modules](https://reader035.fdocuments.us/reader035/viewer/2022080902/56812f69550346895d94f3cb/html5/thumbnails/19.jpg)
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
![Page 20: Python’s Modules](https://reader035.fdocuments.us/reader035/viewer/2022080902/56812f69550346895d94f3cb/html5/thumbnails/20.jpg)
Sources
• http://docs.python.org/library/– Information on Python’s standard library
• http://docs.python.org/tutorial/modules.html– Tutorial on Python’s modules