python module import problem

The following content reference: http://www.xinxingjiaocheng.com/online/item/7/89

1. Give the module an alias

If a module has a very long name, like compute_the_value_of_the_variable, you import the module like this:

import comput_the_value_of_the_variable. After importing, you have to write such a long list of names every time you call the variable or function in it. I will ask you if it bothers you? Especially if you're a minimalist. Even if you start a few letters, pycharm will automatically call out this string for you, and it is estimated that you are unlikely to see it again and again. At this point, we can give it an alias, like this: import comput_the_value_of_the_variable as sky, so you only need to write sky.dongxi when you need to call dognxi inside.

2. Import all contents and import partial contents (individual variables or functions) from a module

Suppose the module name is hello.py, there are variables a, b, c and functions f1(), f2(), f3(), import all functions and variables: from hello import *

Import part of the content: from hello import a,b,f3()

In this case, you don't need to write a module when calling a function. The function name is simply the function name or variable name.

3. Directories as modules

In fact, when importing modules 1 and 2, the default is that the current program is under the same folder as the imported module. If you import a file module that is not under the current folder, an error will occur. Therefore, the correct module import method is to bring the directory name: for example, the file path of lianxi_6.py is: D:\good\s12_1\day2, then when importing the lianxi_6 module (if the current .py file is test4.py, the path Yes: D:\good\s12_1\star, it should be in the same level as the lianxi_6.py file under the parent file directory s12_1) it should be: import day2.lianxi_6 as you, day2 is the folder where the lianxi_6.py file is located name.

import day2.lianxi_6 as you
you.hello()
you.bye()
View Code

The hello() function and bye() function (defined in the lianxi_6.py file):

def hello():
     print ( ' The Yellow River is far above the white clouds, ' )
     print ( ' A lonely city with thousands of mountains. ' )
 def bye():
     print ( ' Why should the Qiang flute complain about the willows, ' )
     print ( ' The spring breeze does not pass through the Yumen Pass. ' )
View Code

If there are many (file) modules in a directory that we need to import, do we have to keep writing from hello import *...? No, at this time we can create a new __init__.py file in this directory (note that there are two underscores), and use this file to unify all the modules that need to be imported. Inside it reads:

from good.a import *

from good.b import *

from good.c import *

a,b,c are the .py files in the good folder, that is, those modules that need to be imported.

At this point the calling method is:

import good
good.f1()
good.f2()
View Code

You can also define the __all__ variable in the __init__.py file, like this:

__all__=['a','b','c']

At this point the calling method is:

from good import *
a.f1()
b.f2()
c.f3()
View Code