Use of modulesLOGIN

Use of modules

1. Import

The use of Python modules is similar to other programming languages. If you want to use a module, you must import it before using it. To import modules we use the keyword import.

The syntax of import is basically as follows:

import module1[, module2[,... moduleN]

For example, we use the math module in the standard library module. When the interpreter encounters an import statement, the module will be imported if it is in the current search path.

#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
import math
_author_ = '两点水'
print(math.pi)
输出的结果:
3.141592653589793

A module will only be imported once, no matter how many times you execute the import. This prevents imported modules from being executed over and over again.

When we use the import statement, how does the Python interpreter find the corresponding file?

This involves Python’s search path. The search path is composed of a series of directory names. The Python interpreter looks for imported modules from these directories in turn. This looks a lot like environment variables. In fact, the search path can also be determined by defining environment variables. The search path is determined when Python is compiled or installed, and should also be modified when installing new libraries. The search path is stored in the path variable in the sys module.

Therefore, we can check the path:

#!/usr/bin/env python
# -*- coding: UTF-8 -*-
import sys
print(sys.path)

Output result:

['C:\Users\Administrator\Desktop\Python\Python8Code', 'G:\PyCharm 2017.1.4\helpers\pycharm', 'C:\Users\Administrator\AppData\Local\Programs\Python\Python36\python36.zip', 'C:\Users\Administrator\AppData\Local\Programs\Python\Python36\DLLs', 'C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib', 'C:\Users\Administrator\AppData\Local\Programs\Python\Python36', 'C:\Users\Administrator\AppData\Local\Programs\Python\Python36\lib\site-packages', 'C:\Users\Administrator\Desktop\Python\Python8Code\com\Learn\module\sys']

2, from···import

Have you ever thought about how to directly import the properties and methods in a module?

In Python, we use the import keyword to import a module. This is importing the module. It should be noted here that this only imports the module and does not import specific items in the module. of a property or method. If we want to directly import a function in a module, that is, attributes and methods, we can use the from···import statement.

The syntax is as follows:

from modname import name1[, name2[, ... nameN]]

After reading the introduction, you may wonder, what is the difference between the from···import and import methods?

Want to know what the difference is, observe the following two examples:

import imports the sys module, and then uses the version attribute

2bd09ec88415bab29f39a8b2035538c.png

##from· ··import directly imports the version attribute

c9ef97b247c9ca52f63fc8b914023ae.png

3. from ··· import *

Through the above learning, We know that from sys import version can directly import the version attribute. But what if we want to use other attributes? For example, if you use executable in the sys module, do you need to write one more sentence from sys import executable? Two is fine, but what about three or four? Do you want to keep writing like this?

At this time, the from ··· import * statement is needed. This statement can import all method attributes in a module. For example:

#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
from sys import *
print(version)
print(executable)

The output result is:

3.6.1 (v3.6.1:69c0db5, Mar 21 2017, 18:41:36) [MSC v.1900 64 bit (AMD64)]
C:\Users\Administrator\AppData\Local\Programs\Python\Python36\python.exe

Note: This provides a simple way to import all method attributes in a module. However, this statement should not be overused.

Next Section

submitReset Code
ChapterCourseware