1

I have a package with files, each file has a class:

model
 -- User.py
 -- Project.py
 -- Item.py
 -- __init__.py

inside __init__.py I have this:

from os.path import dirname, basename, isfile
import glob
modules = glob.glob(dirname(__file__)+"/*.py")
__all__ = [ basename(f)[:-3] for f in modules if isfile(f) and not f.endswith('__init__.py')]

as explained inside this answer: https://stackoverflow.com/a/1057534/2337243

Now my problem is:

I can now use the imported classes in another class but only by doing this:

from model import * 
results = session.query( User.User).all()

My question is: how do I import the model classes and simply call the classes like this:

results = session.query( User).all()

N.B: each file inside model package has one single class that has the same name as the file name.

Ilja Everilä
  • 50,538
  • 7
  • 126
  • 127
Souad
  • 4,856
  • 15
  • 80
  • 140
  • 1
    importing the classes inside the `__init__` file doesn't work? Basically, the following lines inside `__init__`: `from .User import User; from .Project import Project; from .Item import Item`? – Abdou Sep 25 '18 at 15:32
  • Well yeah that's what I was looking for ! thanks – Souad Sep 26 '18 at 06:53
  • 2
    How do I automatically import all these classes ? Instead of having to type `from .User import User` manually? – Julien__ Sep 23 '19 at 09:37

0 Answers0