import function
suggest changeThe __import__() function can be used to import modules where the name is only known at runtime:
if user_input == "os":
os = __import__("os")
# equivalent to import os
The argument is a module name, the same kind of string an import statement uses. A filesystem path is not a module name, so __import__(r"C:/path/to/module.py") does not load that file. To load a module from a file path, use importlib.util. Prefer importlib.import_module() for ordinary dynamic imports.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents