Python Learning summary 6: Modules

Source: Internet
Author: User

module : in Python there is a concept called Module , which simply says that the module is a file that holds the Python code.

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

For example, in Python to call the SQRT function, you must use the import keyword into the math module, the following to understand the Python module. Module file ends with a . py suffix

Import statement

Using the keyword import in Python to introduce a module, such as referencing module math, can be introduced with import math at the very beginning of the file.

When you call a function in the math module, you must refer to this: module name. function Name

Import Math            #  introduces math module print math.sqrt (4)     #  Call function must add module name 2.0
from...import Statements

The FROM statement of Python lets you import a specified section from the module into the current namespace, syntax: from modname import name1[, name2[, ... Namen]]

 from  Import Fibonacci     #  Fibonacci function in the importing module FIB

from...import* statements

It is also possible to import all the contents of a module into the current namespace syntax: frommodname import *

Positioning module

When you import a module, the Python parser's search order for the module location is:

    • Current directory
    • If it is not in the current directory, Python searches for each directory under the shell variable Pythonpath.
    • If none are found, Python looks at the default path. Under UNIX, the default path is typically/usr/local/lib/python/
Pythonpath variable

As an environment variable, Pythonpath consists of many directories that are installed in a single list. The syntax of the Pythonpath is the same as the shell variable path.

In the Windows system, the typical PYTHONPATH is as follows: set pythonpath=c:\python20\lib;

In Unix systems, the typical PYTHONPATH is as follows: set Pythonpath=/usr/local/lib/python

Second, define their own modules

In Python, each python file can be used as a module whose name is the name of the file.

For example, there is a file test.py that defines the function add in test.py:

#test.pydef Add (A, b):    return a+b   

In other files, you can import test first, then call it through Test.add (A, B), and of course, by using the From test import Add.

Three, the characteristics of module import:

(1), loading the execution module, that is, the import module's top-level code will be executed, usually enclosing the module in the global variables, classes and functions of the Declaration.

(2), A module can be imported (import) multiple times, but only load (load) once.


Why use a module?
1, code reuse.
2, the System name space division, is the organization system Components tool.
3, to achieve the sharing of services or data, as long as a module, you can be imported by multiple customers.

Packages in Python

Python has a __init__.py file in each of the module's packages, and with this file we can import the module in this directory.

Parsing package mechanism:

Each of the . Py files is called a module,module can be imported between each other, the module can be defined in the package, Python defines the package in a slightly odd way, suppose we have a parent folder, the folder has a child subfolder , there is a module a.py in child, how do I let python know about this file hierarchy? Very simply, each directory has a file named _init_.py , the contents of which can be empty.

Python Learning summary 6: Modules

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.