Python Module Learning

Source: Internet
Author: User

You can split a code-heavy program into multiple, independent, but interactive pieces of code that are self-contained, organized snippets of code that are modules


Module representation as code file ending with. py


A file is considered a separate module, and a module can also be viewed as a file.


The file name of the module is the name of the module plus the extension. py


Each module has its own namespace


Python program architecture

A python program typically includes a top-level program file and other module files (0, 1, or more)

Top-level files: Contains the main control flow of the program

Module files: Provides a variety of functional components for top-level files or other modules

When the module is first imported (or overloaded), Python immediately executes the top-level program code of the module file, declaring the function object, and the code in the body of the function is not executed until the function is called.

650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M01/70/E9/wKiom1XBRJGz2Zm6AAD7hBgGV_4239.jpg "title=" Qq20150730001634.png "alt=" wkiom1xbrjgz2zm6aad7hbggv_4239.jpg "/>650" this.width=650; "src=" http://s3.51cto.com /wyfs02/m02/70/e5/wkiol1xbs6xywyg0aad7hbggv_4584.jpg "title=" Qq20150730001634.png "alt=" Wkiol1xbs6xywyg0aad7hbggv_4584.jpg "/>

Execution environment of the module

Modules are imported, but modules can also import and use other modules, which can be written in Python or other programming languages


Modules can contain variables, functions, and classes to do their work, and functions and classes can include variables and other elements


If you plan to organize your Python program in multiple program files, only one file can be executed as a top-level file, you can include a lot of program control flow at the top level, and other imported files should contain only classes, variables, functions.

650) this.width=650; "title=" Qq20150730001634.png "src=" http://s3.51cto.com/wyfs02/M01/70/DD/ Wkiol1xanojbco4uaaewfr8htpe275.jpg "alt=" Wkiol1xanojbco4uaaewfr8htpe275.jpg "/>

Import Module

You can only use module names when importing modules, not module filenames with the. py suffix.

Importing the specified module generates a namespace named after the module name.


Impor Statement Import module:

Import Module

You can specify an alias for the namespace when importing, and only use aliases if you use aliases.

Import module as Module_alias

From-import statement:

Commonly used to import only some of the properties of a specified module to the current namespace

From module import name1[,name2,...]

650) this.width=650; "title=" p5bsuj1gqo]i) T ' wgo0z ' iw.png ' src= ' http://s3.51cto.com/wyfs02/M01/70/E0/ Wkiom1xamwlxc7lxaahbida73bi794.jpg "alt=" Wkiom1xamwlxc7lxaahbida73bi794.jpg "/>

Both import and from are implicit assignment statements

Import assigns the entire module object to a variable name

From assigns one or more variable names to an object of the same name in the module that imports the module, overwriting the local if the imported variable and the local variable have duplicate names.

Working mechanism of Import


1, looking for module files, 2, compiled into bytecode, 3, the code to execute the module to create its defined objects. Note: The module will only perform the above steps when it is first imported.

The subsequent import operation is simply to extract the loaded module object in memory, and reload () can be used to reload the module;


Module Search

The Python interpreter must first find the corresponding module file in the Import module

The program's home directory

Python Path Directory

Standard link Library Directory

Contents of any. pth file

These four components are combined to include the path that Sys.path contains, and Python chooses the first file in the search path that matches the import file name.

Import Sys

Sys.path


Sys.path.append ('/tmp/python3/lib ') modifies the Sys.path, adding a search path to the Python library.


To add a test code for a module file











This article from "Breakthrough Comfort zone" blog, reproduced please contact the author!

Python Module Learning

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.