0

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

asked Nov 9, 2010 at 15:13

2 Answers 2

3

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.

answered Nov 9, 2010 at 15:23
Sign up to request clarification or add additional context in comments.

3 Comments

+1 I should have thought of that. Good suggestion, we'll see where it winds up.
Yes, this is a circular import. I need to access a class variable from the class I am trying to import. What would you suggest to get around this? Am I best off rethinking the design a bit?
Well, to get around it you just need to move your import from the top level into a function or class. A redesign might be best but thats your call.
0

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.

answered Nov 9, 2010 at 15:18

6 Comments

This code gives the AttributeError: 'module' object has no attribute my_module7' on the line with the my_name = ...
Can you try this: import my_module1.my_module2.my_module..... dir(my_module1.my_mod....my_module6) obviously fill in the full paths! Let me know what it says
I get an AttributeError on that line now! (the one with the dir(...))
And definitely check out toc's suggestion: you aren't circularly importing stuff are you?
Do your modules import each other?
|

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.