Reputation: 44371
I want to do some package import timing tests. For this, I want to define a list of packages:
packages = [ 'random', 'dateutils', ... ]
for package in packages:
import package
This is of course not working because import
tries to import package "package". How can I tell import
to import the package pointed to by the variable "package"?
Upvotes: 3
Views: 165
Reputation: 879471
for package in packages:
package = __import__(package)
Note that if you are importing a module from a package, such as A.B
,
__import__('A.B')
returns package A
, but __import__('A.B', fromlist = [True])
returns module B
.
Upvotes: 8
Reputation: 640
Read the description of "__import__"
method in the manual may be helpful for you.
Upvotes: 1