Reputation: 10081
I am having some trouble importing a class from a particular module. The class is in the module my_module1.my_module2.my_module3.my_module4.my_module5.my_module6.my_module7
This code works
import my_module1.my_module2.my_module3.my_module4.my_module5.my_module6.my_module7
which means to access my class I have to do
my_module1.my_module2.my_module3.my_module4.my_module5.my_module6.my_module7.MyClass
But this does not
from my_module1.my_module2.my_module3.my_module4.my_module5.my_module6.my_module7 import MyClass
Neither does this
import my_module1.my_module2.my_module3.my_module4.my_module5.my_module6.my_module7 as my_name
Both Give this error saying
AttributeError: 'module' object has no attribute my_module7'
This has me completely stumped and I have been working on it for a couple of weeks now. Any suggestions?
EDIT - I cant change the structure unfortunately as it is imposed by the system I am using
Upvotes: 0
Views: 165
Reputation: 2697
Looks like a circular import.
Gordon McMillan says: Circular imports are fine where both modules use the “import ” form of import. They fail when the 2nd module wants to grab a name out of the first (“from module import name”) and the import is at the top level. That’s because names in the 1st are not yet available, because the first module is busy importing the 2nd.
Upvotes: 3
Reputation: 19044
I think you may want to consider an alternate design in the first place (redesigning your module breakdown so it's a flatter setup), but as that isn't your question try:
import my_module1.my_module2.my_modu...<etc>
my_name = my_module1.my_module2.my_modu...<etc>
my_name.MyClass
A module is a first class object in python, so you can alias them with variables.
Upvotes: 0