can I somehow import all files from one directory at once with one function?
Hello, @FunSkelet. I don’t think that will be useful to do that. But if you want, this code may help you about it:
def importer(path):
import sys,os,importlib
importedFiles = {}
sys.path.append(path)#appends the specific path to the system path for a while.
files = os.listdir(path)
for file in files:
if file.endswith(".py"):
file = file[:-3]
importedFiles.setdefault(file,importlib.import_module(file))
return importedFiles
Usage:
modulesInPath = importer(specificPath)
modulesInPath[moduleName].function()
To make your module -which exists in a specific path- importable, you can just use the line below and after that; you can directly import it.
sys.path.append(somePath)#adds a new path to system path -not permanently
I hope this respond will help you.
I am sorry if there is something wrong about my code.