I have def myfunc()
in script2.py
, script3.py
, script4.py
. In script1.py
, I want to call myfunc
from script2, script3 and script 4.
In script1.py
, rather than tediously listing out:
from script2.py import myfunc
myfunc()
from script3.py import myfunc
myfunc()
from script4.py import myfunc
myfunc()
Is there a way that I can import myfunc
from all scripts present in that same directory?
what you are probably searching for is dynamic import
you can use the __import__
function to call your functions in a loop
for i in range(2, 5):
try:
script_module = __import__("script{}.py".format(i))
script_module.myfunc()
except ImportError:
pass # or do whatever when this fails...
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments