1

I have a file, A.py, it basically looks like this:

import Hasher
hh = Hasher.V2.Cached_V2()

When running, I get the error:

Traceback (most recent call last):
  File "./A.py", line 2, in <module>
    hh = Hasher.V2.Cached_V2()
AttributeError: 'module' object has no attribute 'V2'

On disk, the files layout is like this:

/A.py
/Hasher/__init__.py
/Hasher/V2.py

V2.py contains multiple classes, among them Cached_V2. What's going on? Why isn't the object visible as expected?

1 Answer 1

3

In your A.py, use from Hasher import V2
Basically, when you are just importing 'Hasher' only the init.py file gets loaded.
Reference: Python error: AttributeError: 'module' object has no attribute

Note: In your Hasher/__init__.py file, if you import V2 using from . import V2, then you can use it by directly calling Hasher.V2 from A.py

Sign up to request clarification or add additional context in comments.

1 Comment

That was it. I wasn't importing them in init.py. I'm importing them with import V2, without the from clause. What's the point of using from in this case?

Your Answer

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.