import function

suggest change

The __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.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents