Oscar Mederos
Oscar Mederos

Reputation: 29823

Difference between import and __import__ in Python

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?

Upvotes: 8

Views: 1982

Answers (1)

mgilson
mgilson

Reputation: 309899

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 ...

Upvotes: 9

Related Questions