0

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.

  • Definitely go back to using an `__init__.py`, i know it's confusing but the thing you are doing (pulling out the python internals and doing it yourself), is ... not good. It sounds like what you actually want is making that subfolder of you project a "module" (from the perspective of the `import` statement). Try this: https://stackoverflow.com/questions/15222913/python-imports-from-subfolders – Jessie Koffi May 03 '20 at 16:05
  • Sorry that thread I linked is not great. I'll make an answer for you. – Jessie Koffi May 03 '20 at 16:15
  • Your code is ignoring the value returned from `importlib.import_module(allMods[i])`, which is the loaded module. To make references to te loaded module work, you will need to create a variable from the module's file name and assign it the return value. – martineau May 03 '20 at 16:40

2 Answers2

0

I think you can do

from . import *

but I'm not sure

Lwi
  • 354
  • 4
  • 10
0
from subfolder import filename #no __init__.py necessary
filename.function()

This is one way to do it. This is a confusing topic in python and depends on the details of how exactly you want to set up your project, so your question actually lacks some specific information about that.

Jessie Koffi
  • 174
  • 7
  • I'll edit to make it clearer, but I want to be able not to hardcode subfolder or filename, to import everything I add to a folder dynamically – John Elliott May 04 '20 at 18:24
  • Ah yes, now I get it. Ok, so you were right that the `import` statement does not really support that. Try these: https://stackoverflow.com/questions/1057431/how-to-load-all-modules-in-a-folder – Jessie Koffi May 06 '20 at 10:17