The difference between import, from...import,import...as in Python

Original link: https://blog.csdn.net/colourful_sky/article/details/76114508

 

In python import or from...import is used to import the corresponding module.


1. For example:

import datetime
print(datetime.datetime.now())

 

The above code realizes the output of the current time of the system by introducing the entire datetime package, and then calling the now() method in the datetime class. 
The above code implementation function can also be implemented with the following code:

 

2、

from datetime import datetime
print(datetime.now())

 

 

Here is to import only the datetime class from the datetime package, and then call the now() method in the datetime class to achieve the same purpose.

3、

If you think the name of the datetime package is too long, you want to give it an alias, and replace it with its alias every time you use it in the future, then you need to use import...as, the example is as follows:

import datetime as dt
print(dt.datetime.now())

 

 

The above code completes the same function as before, outputting the current time of the system.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325342175&siteId=291194637