I am writing a function to check if the importing file/module contains any error. The function for some reasons failed when I use the module name as a variable.
# Work
def importfile1():
try:
from test2 import bea
print('1st scenario:', 'import is successful')
except:
print('1st scenario:', 'import failed')
importfile1()
# Does not work
def importfile2(bank):
try:
from test2 import bank
print('2nd scenario:', 'import is successful')
except:
print('2nd scenario:', 'import failed')
importfile2(bea)
I expected both functions to work. However, the second function failed.
The results are shown below:
1st scenario: import is successful
2nd scenario: import failed
May I know why the second function failed?
New contributor
Sam is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.