inside of myfile.py
insert an __import__
module = __import__(sys.argv[1].replace('.py', ''))
will import the first command line argument as module
which you can then use as any other module that was imported. The return value from __import__
is a module.
Really in python, import mod
is just a shorthand for mod = __import__('mod')
, and you are allowed to call the __import__
function if you do so choose.
An example:
>>> module = __import__("math") #same as "import math as module"
>>> module.sqrt(16)
4.0
If you wish to pollute your global namespace with the contents of the command line argument, you can read about how to do a from *
import here:
How does one do the equivalent of "import * from module" with Python's __import__ function?