How do I import files in Python? I want to import:
file.py
)importlib
was added to Python 3 to programmatically import a module.
import importlib
moduleName = input('Enter module name:')
importlib.import_module(moduleName)
The .py extension should be removed from moduleName
. The function also defines a package
argument for relative imports.
In python 2.x:
import file
without the .py extension__init__.py
file__import__
function, which takes the module name (without extension) as a string extensionpmName = input('Enter module name:')
pm = __import__(pmName)
print(dir(pm))
Type help(__import__)
for more details.