Learn how to load Python modules

Source: Internet
Author: User
This article mainly introduces how to load Python modules. For more information, see whether to use import or from mmmm import *. after the program runs, looking back at the mmmm storage. directory of the py file (about mmmm. the py file can be viewed in the previous lecture), with one more file:

qw@qw-Latitude-E4300:~/Documents/ITArticles/BasicPython/codes$ ls mmm*mmmm.py mmmm.pyc
Under this directory, there is an additional mmmm. pyc file besides the original mmmm. py file. Where did this file come from?

To solve this problem, we need to start with the import process.

Import workflow

Import mmmm, instead of simply loading the mmmm. py file to the current location (in the file), is actually the first operation. When mmmm. when py is imported for the first time, python first needs to compile it and generate the extension. pyc files with the same name, and then execute the code of the mmmm module to create corresponding objects. Just like packing an elephant in a refrigerator, you have to perform the following three steps:

Search. That is, python needs to be able to find the import module. How to find it is described later. Compile. Find the module file and compile it into a bytecode, which is in the. pyc File (for the bytecode, we will introduce it below. please continue to read it ). Note: it is not always compiled. it is only compiled during the first run. if the mmmm. py file is changed, it is equivalent to another new file and will be re-compiled. In fact, there is a timestamp in the. pyc file. python will automatically check this timestamp. if it is earlier than the timestamp of the. py file with the same name, it will be re-compiled. Otherwise, Skip. Of course, if the. py source file with the same name is not found, and only the bytecode file. pyc is available, you can only run this file. Run. There is nothing to say about it. the raw rice has been dried up and put in the pot, and it starts to heat up. In the end, it can only be cooked. Execution is the previously compiled module bytecode file, which is executed in a logical unit. Search module

Generally, python automatically completes the module search process. However, in some cases, programmers may be asked to set the search path. After importing a module, python will find the module file to be imported in the following order.

Main Directory of the program. In the previous lecture, run the interactive mode in the "codes" directory. The main directory is "codes". when you run the import mmmm command in that interactive mode, first, search for the corresponding file in the codes directory (find. py and then compiled. pyc ). Of course, in web page programming, the viewer will see that the so-called main directory is a directory that can be set through the top-level file. PYTHONPATH directory. This is an environment variable setting. if it is not set, filter it out. For more information about how to set environment variables, see google. Standard Library Directory. The one that has entered the computer with Python installation. The content of any. pth file. If such files exist, search for them. This is a simple method. in the. pth file, add a valid directory to make it a search path. It is the location where the. pth file is stored on my computer and the. pth file in it.

You can also write your own. pth file, which contains the search directory and save it here. For example, open the easy-install.pth file in the directory and find the content:

Search is such a process. It is recommended that you check the official website. you do not have to set any settings. in many cases, python is automatically completed. Especially for beginners.

Heavy load module

Take the mmmm module as an example (here, I would like to remind you that the name is not good, but it is only for spoof purposes ).

In a shell, python is run and the following operations are performed:

>>> import mmmm
>>> mmmm.web
'https://qiwsir.github.io'

Next I will open a shell, edit the mmmm. py file, and modify it as appropriate:

After saving, switch to the interaction mode of the original imported module:

>>> mmmm.web
'https://qiwsir.github.io'

The output is the same as above, with no changes. why?

It turns out that when the module is imported, the module code will only be loaded and executed during the first import, and the module code will not be re-loaded or re-executed. if the module code is modified, however, the modifications are executed here.

How can we execute a new one after code modification? One way is to exit the original interaction mode, re-enter, and then import mmmm. Haha, this method is a little troublesome. Python provides another function, the reload function, which can be used to reload a module (reload for short) and re-execute the module code after being reloaded. Continue as follows:

>>> reload(mmmm)

>>> mmmm.web
'https://qiwsir.github.io, I am writing a python book on line.'

The modified content is displayed.

Note:

Reload is the built-in function reload (module). module is an existing module, not a variable name.

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.