Difference between import and __import__ in Python

本秂侑毒 提交于 2019-12-23 09:39:36

问题


I was taking a look at some commit of a project, and I see the following change in a file:

-       import dataFile
+       dataFile = __import__(dataFile)

The coder replaced import dataFile by dataFile = __import__(dataFile).

What exactly is the difference between them?


回答1:


import dataFile 

translates roughly to

dataFile = __import__('dataFile')

Apparently the developer decided that they wanted to use strings to identify the modules they wanted to import. This is presumably so they could dynamically change what module they wanted to import ...



来源:https://stackoverflow.com/questions/15401012/difference-between-import-and-import-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!