Hello, I want to import modules from files, I need to store them into variables and I need their names as strings because I must check if the modules python files exist or not for example.
My question is, how to assign a module to a variable even if it's not defined? See the line 1 below, thanks.
To make everything clear, here is another example, I want to store the string 'math' into a variable, then I import it:
My question is, how to assign a module to a variable even if it's not defined? See the line 1 below, thanks.
1 2 3 |
module = math # NameError: name 'math' is not defined module_str = module.__name__ # It should be 'math', but not 'module' because it's not a variable name. import module |
1 |
import 'math' # SyntaxError: invalid syntax |