The Imp module for Python

Source: Internet
Author: User

Imp.get_suffixes ()

Returns a 3-tuple list (suffix, mode, type) for a description of the special module. suffix is the file suffix; mode is the open file model; The type is file types, 1 for Py_source, 2 for py_compiled, and 3 for c_extension

>>> imp.get_suffixes()[(‘.x86_64-linux-gnu.so‘‘rb‘3), (‘.so‘‘rb‘3), (‘module.so‘‘rb‘3), (‘.py‘‘U‘1), (‘.pyc‘‘rb‘2)]
Imp.find_module (name[, Path])
    • If path is empty, the module name is searched according to the Sys.path path, and the ternary group (file, pathname, description) is returned. File is a newly opened module, pathname is the path of the module, description is imp.get_ Suffixes () returns the tuple.
    • If the module is a package, file returns None, Pathname is the package path, and the type returned by description is pkg_directory.
    • Find_module does not process the module name of the hierarchy (with '. ') Module name module.name1.name2).
    • "Path" must be a list.
>>> print imp.find_module(‘os‘)(<openfile‘/usr/lib/python2.7/os.py‘‘U‘at0x7fdeebd53a50‘/usr/lib/python2.7/os.py‘, (‘.py‘‘U‘1))
Imp.load_module (name, file, pathname, description)
    • Load a module that was found by Find_module. If the module has already been loaded, it is equivalent to reload ().
    • When a module is a package or is not loaded from a file, file and pathname can be none and ".
    • Returns a Module object after a successful load, or throws a Importerror exception.
    • Need to close file yourself, preferably with try...finally ...

Instance

>>> file, pathname, desc = imp.find_module(‘os‘)>>> myos = imp.load_module(‘sep‘, file, pathname, desc)>>> ‘sep‘from‘/usr/lib/python2.7/os.pyc‘>>>> myos.getcwd()‘/home/ydoing/github/autorunner‘

The Imp module for Python

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.