I'm trying to create an auto_import function which is part of a library: the purpose of this to avoid listing from .x import y
many times in __init__
files, only do something this import lib; lib.auto_import(__file__)
<- this would search for python files in that folder where the __init__
is present and would import all stuff by exec statement (i.e. exec('from .x import abc')
).
My problem is that, somehow the 'from' statement always tries to import .x from lib directory, even if I change the cwd to the directory where the actual __init__
file is placed... How should I solve this? How should I change the search dir for from .
statement?
Structure:
$ ls -R
.:
app.py lib x
./lib:
__init__.py auto_import.py
./x:
__init__.py y
./x/y:
__init__.py y.py
e.g.: ./x/y/__init__.py
contains import lib; lib.auto_import(__file__)
auto_import is checking for files in dir of __file__
and import them with exec('from .{} import *')
(but this from . is always the lib folder and not the dir of __file__
, and that is my question, how to change this to dir of __file__
Of course the whole stuff is imported in app.py like:
import x
print(x.y)
Thanks
EDIT1: final auto_import (globals() / gns cannot be avoided )
import os, sys, inspect
def auto_import(gns):
current_frame = inspect.currentframe()
caller_frame = inspect.getouterframes(current_frame)[1]
src_file = caller_frame[1]
for item in os.listdir(os.path.dirname(src_file)):
item = item.split('.py')[0]
if item in ['__init__', '__pycache__']:
continue
gns.update(__import__(item, gns, locals(), ['*'], 1).__dict__)