2

Suppose I provide a module in the command line and want to import it using the "imp" module:

$ foo.py mod.a.b.c

What is the proper way of doing this?

Split the "mod.a.b.c" and add each path? The behaviour of "imp" does not seem to be parallel to "import".

mathtick
  • 6,487
  • 13
  • 56
  • 101

1 Answers1

5

Given a module path as a string (modulename), you can import it with

module = __import__(modulename,fromlist='.') 

Note that __import__('mod.a.b.c') returns the package mod, while __import__('mod.a.b.c',fromlist='.') returns the module mod.a.b.c.

unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677