0

I got the following files architecture:

[X] publisher/
    [X] __init__.py (containing Publisher(Object), an abstractclass)
    [X] mail.py (containing class Mail(Publisher))
    [X] simple.py (containing class Simple(Publisher))
    [X] ftp.py (containing class Ftp(Publisher))
[X] app.py

In app.py I got this dictionary :

publisher_to_load = ["Mail","Mail","Simple"]

I would like to instanciate, for each publisher_to_load, the corresponding publisher.

I tried :

import publisher
for name in publisher_to_load:
    getattr(publisher, name)()

But I got the error

AttributeError: 'module' object has no attribute 'Simple'

Any idea ?

Blusky
  • 3,470
  • 1
  • 19
  • 35

1 Answers1

0

As may have been mentioned, your problem is the imports in __init__.py. That should solve your problem. However, given the general title of the question I will also note a few more things for anyone else. First of all, if you do in fact think you have to have to make things import each other like in the question alex referenced, first consider a better option to make two programs cooperate. If you need two-way communication, open up a socket or design some other kind of intermediary hand-off of information, two programs importing each other is a truly messy way to make two modules cooperate. Secondly, accessing variable names with strings should be avoided if possible. Especially in this case, that is not how handling imports should work. That being said, gatattr (or object.__dict__[]) like the question uses should work just fine.

trevorKirkby
  • 1,886
  • 20
  • 47