Python Modules, Schemes and Mind Maps of Web Programming and Technologies

Creating a script file of your Python input for each individual program would be cumbersome and ... The “sys” module is needed to access the command line.

Typology: Schemes and Mind Maps

2022/2023

Uploaded on 03/01/2023

bairloy
bairloy 🇺🇸

4.2

(6)

247 documents

1 / 12

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
PYTHON MODULES
Brandon McKay
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Python Modules and more Schemes and Mind Maps Web Programming and Technologies in PDF only on Docsity!

PYTHON MODULES

Brandon McKay

Reasons for using modules

  • Definitions of functions and variables are lost after quitting each session of the Python interpreter
  • Creating a script file of your Python input for each individual program would be cumbersome and redundant
  • Modules allow you to import code from an external file and use it in your program/script, in a similar fashion as libraries used in C++

Importing modules (cont.)

  • Can import selected portions of the module
  • Import selected names in the module as local names

from module import function1, function function1(argument) function2(argument)

  • Import all names except those that start with “_”

from module import *

Executing modules as scripts

  • When importing modules, the name variable is set to the module’s name
  • When running a module directly from the command line, the name variable is instead set to main
  • This allows the ability to execute code depending on whether the module is being used as a script or as an imported module

$ python module.py arguments (name=“main”)

import module (name=“module”)

Searching for modules

  • When a module is imported, the python interpreter first looks for a built-in module
  • If not found, it will then search for “moduleName.py” in the locations defined by the variable “sys.path”, which initially defines these locations: - the directory containing the script currently running - the locations defined in “PYTHONPATH” - the default installation directory
  • Python programs can modify “sys.path” after initialization

Pre-compiled Python files

  • Python caches the compiled versions of each module,

allows much faster execution of programs

  • Stores in “pycache” directory, under the filename

“moduleName.version.pyc”

  • This ensures that the compiled versions are used with the

correct version/release of Python being used

  • Automatically recompiles any source file that is modified
  • Python always recompiles modules ran from the

command line, however

The built-in dir() function

  • Is used to find out which names are defined by a module
  • Returns the names as a sorted list of strings

import module dir(module) [‘name’, ‘function1’, ‘function2’] a = 5 dir() [‘builtins’, ‘name’, ‘module’, ‘a’, ‘function1’, ‘function2’]

  • When called without an argument, dir() lists all currently defined names

Packages

  • A package is a collection of modules
  • A file named “init.py” must be placed in the directory of the package, to indicate that the directory is indeed a package
  • init.py” can also contain executable code
  • Packages can contain other packages, or “subpackages”

import package import package.subpackage.function package.subpackage.function(argument) from package.subpackage import function function(argument)