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
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"])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With