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.

3
  • 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: stackoverflow.com/questions/15222913/… Commented May 3, 2020 at 16:05
  • Sorry that thread I linked is not great. I'll make an answer for you. Commented May 3, 2020 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. Commented May 3, 2020 at 16:40

2 Answers 2

0

I think you can do

from . import *

but I'm not sure

answered May 3, 2020 at 15:58
Sign up to request clarification or add additional context in comments.

Comments

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.

answered May 3, 2020 at 16:17

2 Comments

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
Ah yes, now I get it. Ok, so you were right that the import statement does not really support that. Try these: stackoverflow.com/questions/1057431/…

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.