Import modules in python


What is the difference between importing a module with import nombremodulo and from nombremodulo import * ?

Following several books I have noticed that there are modules that import them in the first way, such as the module os , but not other modules, such as datetime . I have tried to import datetime of the first form and it does not let me access the functions / methods of this module. For this I must import it in the second way.

asked by Jogofus 22.12.2016 в 03:59

1 answer


The difference of import module or from module import foo is not very relevant, since in both cases the module integer is imported.

The only difference between these ways is the name that is associated with the current environment.

import module adds the name of module while from module import foo adds foo .

On performance, there are no different.

In your specific case of datetime it is not about not allowing you to use datetime if you do not use it with from datetime import datetime but there are 2 things, the module datetime and the class datetime within the module, which are different things in this case, here the documentation

answered by 22.12.2016 в 04:44