The following information is referenced in: http://www.xinxingjiaocheng.com/online/item/7/89
1. Give the module an alias
If the name of a module is long and long, like this comput_the_value_of_the_variable, you import the module like this:
Import comput_the_value_of_the_variable, every time after importing the variables or functions inside the time to write a long list of names, I asked you bored? Especially when you are a minimalist person. Even if you lead a few letters, Pycharm will automatically pull you out of this string, it is estimated that you are not much like seeing it again and again. At this point we can give it an individual name, such as: Import comput_the_value_of_the_variable as Sky, so you need to call inside the Dognxi when you just write Sky.dongxi.
2. Import all content and import parts from a module (individual variables or functions)
Assuming that 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 content: from Hello import a,b,f3 ()
In this case, you do not need to write the module when calling the function . function name, directly godless or variable name on the line.
3. Directory as a module
In fact, 1 and 2 in the Import module is the default current program with the imported module under the same folder, if you import a file module that is not under the current folder, the error will occur. So, the correct way to import the module is to bring the directory name: for example, the lianxi_6.py file path is: D:\good\s12_1\day2, then import the Lianxi_6 module (if the current. py file is test4.py, the path is: D:\good\ S12_1\star, it should be under the same level of lianxi_6.py file as the parent file directory s12_1) in this jiangzi: import day2.lianxi_6 as You,day2 is the name of the folder where the lianxi_6.py file resides.
Import Day2.lianxi_6 as Youyou.hello () You.bye ()
View Code
The hello () function and the bye () function (defined in the lianxi_6.py file):
def hello (): print (" Yellow River far above Baiyun, " ) print ( Span style= "COLOR: #800000" > " a piece isolated city Ren Mountain. " ) def bye (): print ( ' Qiang flute Why blame Willow, " " print ( " Spring Breeze not degree yumen guan. ")
View Code
If there are many (file) modules in a directory that we need to import, we will always write from Hello Import * ... It? No, at this point we can create a new __init__.py file in this directory (note that the underscore is two), use this file to all the modules need to be imported unified together. It reads:
From GOOD.A Import *
From good.b Import *
From GOOD.C Import *
A,b,c is a. py file under the Good folder, which is the module that needs to be imported.
The method that is called at this time is:
Import goodgood.f1 () good.f2 ( )
View Code
You can also define the __all__ variable in the __init__.py file, so write:
__all__=[' A ', ' B ', ' C ']
The method that is called at this time is:
from Import *a.f1 () b.f2 () c.f3 ()
View Code
Module import issues for Python