Python Review and collation 10: Modules

Source: Internet
Author: User

0. Description


Modules are used to organize Python code methods, while packages are used to organize modules, making the best use of packages and modules will facilitate the development of large, clearly structured programs.



1. What is a module


The so-called module, in fact, is a. py file that contains a specific function code, in this. py file, the following code types are mainly:

    • Classes that contain data members and methods

    • A set of related but independent operation functions

    • Global variables

You can import related properties in a module by using the import statement.



2. Modules and files


A module is a logical way to organize Python code, but on a physical level, it is a file, so a file is considered a standalone module, and a module can be viewed as a file. The file name of the module is the name of the module plus the extension. py.

Unlike other languages in which classes can be imported, module or module properties are imported in Python.


(1) Module name space

Namespaces are a very important concept of python, and the so-called namespaces, in fact, refer to a collection of relational mappings of names to Objects . Because each module defines its own unique namespace, there is no name crossover between modules, and through the period property, even if there are variables of the same name in two modules, no name collisions will occur due to different module names.


(2) Search path and path search

The import of modules (using the import statement) requires a process called path search, in which the file system "predefined areas" are searched for the module files to be imported, and these predefined areas are actually collections of Python search paths, and there are two concepts to note:

    • path Search : Refers to the action of finding a file, which is a verb

    • Search Path : A set of directories to look for, a noun

If the module name is not in the search path, the importerror exception is triggered:

>>> Import Mymodulestraceback (most recent): File ' <stdin> ', line 1, in <module>importerro R:no module named Mymodules

The default search path is specified at compile or install time and can be modified in two places:

    • Set environment variable Pythonpath

    • Add a search path in Sys.path

When the Python interpreter is started, the search path is saved in the Sys.path variable of the SYS module:

>>> import sys>>> sys.path[', '/usr/local/lib/python2.7/dist-packages/pip-8.0.2-py2.7.egg ', '/ Usr/local/lib/python2.7/dist-packages/setuptools-3.3-py2.7.egg ', '/usr/lib/python2.7 ', '/usr/lib/python2.7/ Plat-x86_64-linux-gnu ', '/usr/lib/python2.7/lib-tk ', '/usr/lib/python2.7/lib-old ', '/usr/lib/python2.7/ Lib-dynload ', '/usr/local/lib/python2.7/dist-packages ', '/usr/lib/python2.7/dist-packages ', '/usr/lib/python2.7/ Dist-packages/pilcompat ', '/usr/lib/python2.7/dist-packages/gtk-2.0 ', '/usr/lib/python2.7/dist-packages/ Ubuntu-sso-client ']

Returns a list with the first element representing the current directory. You can increase the search path by adding elements to this list (using append or insert):

>>> Import Mytraceback (most recent): File ' <stdin> ', line 1, in <module>importerror:no m Odule named My>>> sys.path.append ('/home/xpleaf/test ') >>> import my

If there are multiple identical module names, the Python interpreter uses the first module found along the search path order.

Also use Sys.modules to find which modules are currently imported and where they come from, as follows:

>>> sys.modules{' copy_reg ': <module ' copy_reg ' from '/usr/lib/python2.7/copy_reg.pyc ';, ' Sre_compile ' : <module ' sre_compile ' from '/usr/lib/python2.7/sre_compile.pyc ' >,...}

As you can see, unlike Sys.path, Sys.modules returns a dictionary where key is the name of the module and the key value is the path to the module.











This article from "Fragrant fluttering leaves" blog, reproduced please contact the author!

Python Review and collation 10: 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.