Python advanced 4 _ module call

Source: Internet
Author: User

1. Basic Introduction 1. Basic syntax

The import keyword is used for module import.

Simply put, every Python source code file ending with the extension py is a module. Other files can be imported into one module to read the content of this module. In essence, it is to load another file, and read the content of that file. It should be noted that after the first import, other repeated imports will not work, or even the source code files of the module changed and saved in another window will not work. This is because the import is a very costly operation, so that each file, each program running cannot be redundant once, if the file needs to be run again in the same session, call the reload function in the imp standard module.

The following is a simple example of calling and printing the system path:

2. How to import a custom Module

Follow these steps:

1. Create a workspace and add the path to the system path. <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + kernel/6TwvcD4KPHA + My7N4rK/xKO/kernel + kernel/CvM/Cu + kernel + ttbQo6zI58/Co7o8L3A + kernel = "http://www.2cto.com/uploadfile/Collfiles/20140311/20140311085959369.jpg" alt = "\">

This method has advantages and disadvantages. This method is only suitable for temporarily adding a path. That is to say, when you restart the command line window, the path is no longer available. Do not believe it, another permanent method is to add it directly to the path in the environment variable, so you don't have to worry about it every time.

2. define your own modules

As a test module, we simply write a computing function:

3. External module call


From this we can see that, in addition to directly importing the python module and then calling the file name. function name, we can also use the following method when calling an external module,

The from File import function saves the prefix of the scope when calling, but now there is another problem. If the module I want to import has several layers of files, for example, what should I do if I want to import the Calculate function in \ Test \ test1 \ test2 \ test3.py? Let's look at it again. (assume you already have E: \ Coding \ Test \ test1 \ test2 \ test3.py)

It can be seen that when there are several layers of files, if direct import is performed, the layers are used. it is inconvenient to include all the hierarchical files when referencing. So we can use import... as... to simplify this expression, or directly use a function from... import ..., you can use it directly.

3. Several issues that need to be considered 1. Module change issues

For example, if the import module needs to be changed after the function is executed, how can we solve this problem? Suppose we need to add a prompt before the calculation result:

Will the imported modules continue to work? Let's take a look:

We will find that the prompt is not printed! What should I do at this time? In this case, the reload function is used, which is located in the imp package in python3:

2. How does Import work?

About how import works, we can basically divide it into three stages:

1. Find the module File

2. compile it into a bit code (when necessary)

3. Code of the execution module

Note that these three steps will only be executed when the module is imported for the first time, and will be skipped when the same module is imported, only extract the loaded module objects in the memory.

Previously, the search involves mainly searching by program based on the system path. After that, the source code is compiled into bytecode, and python checks the timestamp of bytecode. If it is earlier than the source code, the bytecode is re-compiled. pyc. Otherwise, the bytecode is loaded directly. The last step is to execute the bytecode in sequence, that is, when the module is imported for the first time, it will execute the bytecode of the module, as shown below, we add a print function:

Open the shell editor again, as shown below:

After the modification, add a print statement:

At this time, let's reload it and import it again:

These results validate our previous statements.

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.