I'm a python beginner (couple of years of classes and a life of experimenting) returning for a personal project.
I want to import everything from a folder, I want all the code I add to the folder to be imported to my main. After researching I came up with this attempt, placed on main:
from os.path import dirname, basename, isfile, join
import glob
import importlib
modules = glob.glob(join(dirname(__file__), "*.py"))
allMods = [ basename(f)[:-3] for f in modules if isfile(f) and not f.endswith('__init__.py') and not f.endswith("main.py")]
for i in range(0, len(allMods)):
importlib.import_module(allMods[i])
'allMods' has all the names of the files in an array, - ["a","b","etc"] - which is why I use the for loop to go through every one of them and try to import them - example: importlib.import_module("a") There are no errors at this point, so something is working, but when trying to reach a.py, b.py, etc.py they are simply not defined ("unresolved reference").
Before this, I tried everything from the standard import to wild code at _ _ init _ _.py, I even got to the 2nd page of google.
This is clearly too advanced for my current skill (trial and error is how I learn best). So, is this solution wrong from the start, or is there anything salvageable?
Thanks.
EDIT_1: Basically, I want to be able to import everything I add to a folder dynamically and not to hardcode import commands.