43. Module from import

One from... import

1.1 from... import

from ... import ... Use examples of.

from meet import name, read1
print(name)
read1()
'''
Execution results:
from the meet.py
 Taibai Venus
meet Module: Guo Baoyuan
'''

1.2 comparison between from... Import... And import

The only difference is that using from... Import... Is to import the names in spam directly into the current namespace, so in the current namespace, you can use the names directly without prefix: tbjx

from... import... Has both advantages and disadvantages

Benefits: easy to use

Disadvantage: it is easy to conflict with the name in the current execution file

Example demonstration:

  1. If the executable file has a variable or function name with the same name as the module, it will have an overwrite effect.
name = 'oldboy'
from meet import name, read1, read2
print(name)  
'''
Execution results:
Guo Baoyuan
'''
----------------------------------------
from meet import name, read1, read2
name = 'oldboy'
print(name)  

'''
Execution results:
oldboy
'''
----------------------------------------
def read1():
    print(666)
from meet import name, read1, read2
read1()

'''
Execution results:
meet Module: Guo Baoyuan
'''
----------------------------------------

from meet import name, read1, read2
def read1():
    print(666)
read1()

'''
Execution results:
meet Module: 666
'''

2. Just use read1 and read2 directly for the current location. When executing, the global namespace of the meet.py file is still used

#Test 1: when the imported function read1 is executed, it still returns to meet.py to find the global variable 'alex'
#test.py
from meet import read1
name = 'alex'
read1()
'''
results of enforcement:
from the meet.py
meet->read1->name = 'Guo Baoyuan'
'''

#Test 2: when the imported function read2 is executed, it needs to call read1() and still go back to meet.py
#read1()

#test.py
from meet import read2
def read1():
    print('==========')
read2()

'''
results of enforcement:
from the meet.py
meet modular
meet Module: Guo Baoyuan
'''

1.3 from... import also supports as

You can also rename a module by referencing it in this way.

from meet import read1 as read
read()

1.4 importing multiple documents in one line

from tbjx import read1,read2,name

1.5 from ... import *

from meet import * import all names in meet that do not start with an underscore () into the current location

In most cases, our python programs should not use this import method, because * you don't know what name you import, and it is likely to overwrite the name you have defined before. Moreover, the readability is extremely poor, and there is no problem when importing in an interactive environment.

You can use all to control * (used to release a new version) and add a new line in meet.py

__all__=['name','read1'] #In this way, the two names specified in the list can be imported by using from spam import * in another file

1.6 module cycle import problem

The root cause of the exception thrown by module circular / nested import is that after the module is imported once in python, it will not be re imported, and only the code in the module will be executed during the first import

Circular / nested import should be avoided as far as possible in our project. If there is data that needs to be shared by multiple modules, the shared data can be stored in one place. The exception analysis and solutions after circular / nested import in the program are as follows (understand and avoid it as far as possible in the future)

The content of the model document is as follows

#Create an m1.py
print('Importing m1')
from m2 import y
x='m1

#Create a m2.py
print('Importing m2')
from m1 import x
y='m2'

#Create a run.py
import m1

#Test one
 implement run.py Will throw an exception
 Importing m1
 Importing m2
Traceback (most recent call last):
  File "/python project/run.py", line 1, in <module>
    import m1
  File "/python project/m1.py", line 2, in <module>
    from m2 import y
  File "/python project/m2.py", line 2, in <module>
    from m1 import x
ImportError: cannot import name 'x'

#Analysis of test results
 Execute first run.py--->implement import m1,Start import m1 And run its internal code--->print contents"Importing m1"
--->implement from m2 import y Start import m2 And run its internal code--->"The print content" "is being imported." m2"--->implement from m1 import x,because m1 It has been imported, so it will not be re imported, so go directly m1 Zhongna x,however x It does not exist at this time m1 Medium, so an error is reported

#Test 2: executing the file is not equal to importing the file. For example, executing m1.py is not equal to importing M1
 Importing m1
 Importing m2
Traceback (most recent call last):
Importing m1
  File "/python project/m1.py", line 2, in <module>
    from m2 import y
  File "/python project/m2.py", line 2, in <module>
    from m1 import x
  File "/python project/m1.py", line 2, in <module>
    from m2 import y
ImportError: cannot import name 'y'


#Test II analysis
 implement m1.py,Print "importing" m1",implement from m2 import y ,Import m2 Then execute m2.py Internal code--->Print"Importing m2",implement from m1 import x,here m1 This is the first time it is imported and executed m1.py It doesn't mean import m1,The import begins m1 And execute its internal code--->Print"Importing m1",implement from m1 import y,because m1 It has been imported, so you don't need to continue importing m2 want y,however y It does not exist at this time m2 Medium so error is reported


# resolvent:
Method 1:Import statement last
#m1.py
print('Importing m1')

x='m1'

from m2 import y

#m2.py
print('Importing m2')
y='m2'

from m1 import x

Method 2:Put the import statement into the function
#m1.py
print('Importing m1')

def f1():
    from m2 import y
    print(x,y)

x = 'm1'

# f1()
#m2.py
print('Importing m2')

def f2():
    from m1 import x
    print(x,y)

y = 'm2'

#run.py
import m1

m1.f1()

Keywords: Python Pycharm Flask

Added by warewolfe on Mon, 27 Sep 2021 01:05:11 +0300