3

I want to allow the user to import a module based on their string input argument.

i.e

$python myprogram.py --import_option xyz.py

In python I want something like this after parsing an argument.

arg = 'xyz.py'

import arg #assuming that the module name xyz.py exists.
Dhruv Patel
  • 386
  • 3
  • 6
  • 19

1 Answers1

9
>>> a='math'
>>> import importlib
>>> i=importlib.import_module(a)
>>> i.sqrt(4)
2.0
g4ur4v
  • 3,130
  • 5
  • 29
  • 56