Python module

Python Module (Module) is a Python file that ends in .py and contains Python object definitions and Python statements.

The

module allows you to logically organize your Python code snippets.

Assigning the relevant code to a module will make your code better and easier to understand.

Modules can define functions, classes, and variables, and modules can also contain executable code.

Examples

The following example is a simple module support.py:

support.py Module:

def print_func( par ): print "Hello : ", par return

import statement

Introduction of modules

After the module is defined, we can use the import statement to introduce the module with the following syntax:

import module1[,< /span> module2[,... moduleN]]

For example, to reference the module math, you can use import math at the beginning of the file to introduce it. When calling a function in the math module, it must be referenced like this:

module name.function name

When the interpreter encounters an import statement, the module will be imported if it is in the current search path.

The search path is a list of all the directories that the interpreter will search first. If you want to import the module support.py, you need to put the command at the top of the script:

test.py file code:

#!/usr/bin/python # -*- coding: UTF-8 -*- # Import Module import support # Now you can call the functions contained in the module support.print_func< Span class="hl-brackets">("Runoob")

The above example output:

Hello : Runoob

A module will only be imported once, no matter how many times you perform an import. This prevents the import module from being executed over and over again.



From...import statement

Python's from statement lets you import a specific part from the module into the current namespace. The syntax is as follows:

from modname import name1[, name2 [, ... nameN]]

For example, to import the fibonacci function of the module fib, use the following statement:

from fib import fibonacci

This declaration does not import the entire fib module into the current namespace. It only introduces fibonacci in the fib to the global symbol table of the module that executes the declaration.



from...import* statement

It is also possible to import all the contents of a module into the current namespace, just use the following statement:

from modname import *

This provides an easy way to import all the items in a module. However, such a statement should not be used too much.

For example, we want to introduce everything in the math module at once, with the following statement:

from math import *

Search path

When you import a module, the Python parser searches for module locations in the following order:

  • 1, current directory
  • 2. If not in the current directory, Python searches for each directory under the shell variable PYTHONPATH.
  • 3. If you can't find it, Python will look at the default path. Under UNIX, the default path is usually /usr/local/lib/python/.

...... The module search path is stored in the sys.path variable of the system module. The variable contains the current directory, PYTHONPATH and the default directory determined by the installation process.



PYTHONPATH variable

As an environment variable, PYTHONPATH consists of a number of directories housed in a list. The syntax of PYTHONPATH is the same as the shell variable PATH.

On Windows systems, the typical PYTHONPATH is as follows:

set PYTHONPATH=c:\python27\lib;

On UNIX systems, the typical PYTHONPATH is as follows:

set PYTHONPATH=/usr/local/lib/python


Namespaces and Scopes

The

variable is the name (identifier) of the matching object. A namespace is a dictionary that contains variable names (keys) and their corresponding objects (values).

A Python expression can access variables in local namespaces and global namespaces. If a local variable and a global variable have the same name, the local variable overrides the global variable.

Each function has its own namespace. The scope rules of a class's methods are the same as for a normal function.

Python intelligently guesses whether a variable is local or global, assuming that any variables assigned within a function are local.

Therefore, if you want to assign a value to a global variable within a function, you must use the global statement.

The expression for global VarName tells Python that VarName is a global variable, so Python won't look for this variable in the local namespace.






javacodegeeks is optimized for learning.© javacodegeeks
All Right Reserved and you agree to have read and accepted our term and condition.