Introduction to the principles and methods of Python module lookup

Source: Internet
Author: User
This article mainly introduces the principles and methods of the module lookup in Python, the text of the sample code introduced in very detailed, to everyone's study or work has a certain reference learning value, the need for friends to follow the small series to learn together.

Objective

This article mainly introduces to you about the Python module to find the principle and way, share out for everyone to reference the study, the following words do not say, come together to see the detailed introduction:

Basic concepts

Module

module, a py file or other file form can be imported is a module

Package

Package containing the folder with the __init__ file

Relative path

Relative path, relative to the path of a directory

Absolute path

Absolute path, full path

Path Lookup

Python interpreter finds the package or module being introduced

How the Python interpreter looks for packages and modules

Python executes a py file, regardless of whether it is performed in the absolute or relative path, the directory in which the file resides is added to the sys.path list, and Python is the inside of the interpreter that looks for the sys.path package and module. sys.path and the environment variables of the Python are decided by itself.

Code-1


#test. Pyimport osimport sysprint sys.path[0]# Executepython test.pypython/users/x/workspace/blog-code/p2016_05_28_ python_path_find/test.py

Execution indicates that both the relative and absolute paths output the same results, and regardless of the execution method, the folder where the test.py is located is the first to be added, that is sys.path , the location of index 0.

What is the order in which the Python interpreter looks for packages

The interpreter finds the package, searches first for the built-in module, followed by sys.path the search order, which causes the package or module with the same name to be obscured.

Code-2


#ls ├──os.py├──test2.py├──redis.py#test2.pyimport osfrom redis import Redis#execute test2.pytraceback (most recent call Last): File '/users/x/workspace/blog-code/p2016_05_28_python_path_find/test2.py ', line 1, in <module> from Redis Import redisimporterror:cannot Import Name Redis

Since the OS is the built-in module, even with the same name modules in the same directory, the interpreter can still find the correct OS module, confirming that the built-in module will not be obscured, and Redis is a third-party module, and the default installation location is s in the Python environment variable Ite-packages, the interpreter will add the contents of this directory after launch, sys.path because the current directory will be in sys.path the first place, Redis priority is found in the current directory, and the Redis module in Site-packages is obscured.

The lookup order of the interactive execution environment

Into the interactive execution environment, the interpreter automatically joins the current directory sys.path , in which case the current directory is in the form of a relative path sys.path :


>>> Import os.path>>> Import sys>>> Os.path.abspath (sys.path[0]) '/users/x/workspace/ Blog-code ' >>>

In addition, the other is the same as executing a file.

__file__ variables in the module

__FILE__ is the pathname of the file from which the module was loaded, if it were loaded from a file. If a module is loaded from a file, __file__ is the path name of the module –python DOC:

As the name implies, when the module appears as a file __file__ refers to the path name of the module file, the relative path to execute __file__ is the relative path, the absolute path to execute __file__ is absolute.


#test3. Pyprint __file__# relative path execution python test3.pytest3.py# absolute path execution python/users/x/workspace/blog-code/p2016_05_28_ python_path_find/test3.py/users/x/workspace/blog-code/p2016_05_28_python_path_find/test3.py

In order to ensure that the __file__ can accurately get the correct position of the module every time, it is best to take the absolute path again os.path.abspath(__file__) .

__file__ in an interactive shell


>>> __file__traceback (most recent): File "<input>", line 1, in <module>nameerror:name ' __ File__ ' is not defined

This is because the execution of the current interactive shell is not loaded as a file, so there is no such property as __file__.

SYS.ARGV[0] Variable

sys.argv[0] is the execution file that it uses to get the main entry.


#test. Pyimport sysprint __file__print sys.argv[0]

The above print outputs the same result because the main execution file and the module that the __file__ belongs to are the same, and when we change the entry file, the difference arises.


#test. Pyimport sysprint __file__print sys.argv[0] #test2. Pyimport Test#execute test2.py/users/x/workspace/blog-code/ p2016_05_28_python_path_find/child/test.py #__file__test2. py #sys. argv[0]

In general, sys.argv[0] it is the path to the access execution file, __file__ is the path to the arbitrary module file.

The role of Sys.modules

Since Python is sys.path searching for modules in, where are the loaded modules stored? The answer is sys.modules . Once the module is loaded, Python will add the module to the next load, which will speed up the introduction of the module sys.modules and play a role in caching.


>>> import sys>>> sys.modules[' Tornado ']traceback (most recent call last): File "<input>", line 1 , in <module>keyerror: ' Tornado ' >>> import tornado>>> sys.modules[' tornado ']<module ' Tornado ' from '/USERS/X/PYTHON_DEV/LIB/PYTHON2.7/SITE-PACKAGES/TORNADO/__INIT__.PYC ' >

As mentioned earlier, after the Python interpreter is started, it is pre-loaded into the built-in module, which can be sys.modules verified.


>>> sys.modules[' os ']<module ' OS ' from '/users/x/python_dev/lib/python2.7/os.pyc ' >>>>

With sys.modules and __file__, you can dynamically get all loaded module catalogs and paths.


>>> Import os>>> Os.path.realpath (sys.modules[' OS '].__file__) '/users/x/python_dev/lib/python2.7 /os.pyc ' >>> import tornado>>> os.path.realpath (sys.modules[' tornado '].__file__) '/users/x/python _dev/lib/python2.7/site-packages/tornado/__init__.pyc '


def get_module_dir (name): Path = GetAttr (Sys.modules[name], ' __file__ ', None) if not path raise Attributeerror (' module%s Have not attribute __file__ '%name) return Os.path.dirname (Os.path.abspath (path))

Summary

In general, Python determines the import of a package by looking up Sys.path , and the System Package priority > same directory; Sys.path , unique properties in Python __file__ and sys.argv[0] , sys.modules can help us understand the concept of package lookup and import, as long as the role and behavior of Sys.path are properly understood, Understanding the search for a package is not a problem.

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.