introduce
In Python, use import or from...import to import the corresponding module. The so-called module is actually a collection file of some functions and classes. It can implement some corresponding functions. When we need to use these functions, we can directly import the corresponding module into our program and we can use it. This is similar to the include header file in C language. In Python, we use import to import the modules we need.
So what is the difference between these two methods? ? ?
- import module: Import a module;
- from…import: Import a function in a module; Note: It is equivalent to importing a file in a folder, which is an absolute path
So the difference in usage is that when referring to a file, it is:
import //Module. Functionfrom…import // Just use the function name directly
Give an example
Assume that there are the following functions in the module:
def print_func( par ): print "Hello : ", par return
Use the import module
The correct way to introduce and call the support module:
# Import moduleimport support # You can now call the functions contained in the modulesupport.print_func("Runoob")
Notice: You cannot directly use print_func() to implement the call. You must treat the introduced module name as an object and call the method print_func under this module object. Only then can you realize the call.
Use the from … import module
# Import modulefrom support import * # You can now call the functions contained in the moduleprint_func("Runoob")
Notice: You can directly use print_func() to implement the call.
suggestion
Generally speaking, it is recommended to use import statements to avoid using from … import, because this will make your program more readable and also avoid name conflicts.
This is the end of this article about the difference between from…import * and import in Python. For more related Python from…import * and import content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!