5

I want to import all python modules in a directory in python, and came across this snippet:

for name in os.listdir("modules"):
    if name.endswith(".py"):
        module = name[:-3]   # remove extension name for convenience.
        globals()[module] = __import__(os.path.join("modules", name))

program outputs:

ImportError: Import by filename is not supported.

How to I import file by filename?

Sajuuk
  • 2,667
  • 3
  • 22
  • 34
  • To import all Python modules in a directory, use an `__init__.py` file. See this: https://stackoverflow.com/questions/1057431/loading-all-modules-in-a-folder-in-python [duplicate] – crazyGamer Aug 09 '17 at 06:42

0 Answers0