how to import all files from different folder in Python

Viewed 16139

with __init__.py in the directory, I was able to import it by

from subdirectory.file import *

But I wish to import every file in that subdirectory; so I tried

from subdirectory.* import *

which did not work. Any suggestions?

3 Answers

Found this method after trying some different solutions (if you have a folder named 'folder' in adjacent dir):

for entry in os.scandir('folder'):
    if entry.is_file():
        string = f'from folder import {entry.name}'[:-3]
        exec (string)
Related