Reputation: 1951
I have this module (called module1.py):
import os
def main():
command=os.system("dir")
return command,"str"
I have dynamically imported it with this:
mod = __import__("modules."module1)
It works great. But now I want to call the function "main" of module1.
mod.main()
does not work. Why?? How may I call the main() function of the module1 module?
Thank you very much
Upvotes: 4
Views: 2555
Reputation: 132138
I prefer using the fromlist
argument.
mod = __import__("modules.%s" % (module1), fromlist=["main"])
mod.main()
Depending on your use case you may also want to specify locals and globals.
mod = __import__("modules.%s" % (module1), locals(), globals(), ["main"])
Upvotes: 5