7

I create two files: test.py and test1234.py

test.py contains:

import test1234
t = test1234.test()

test1234.py contains:

class test():
    def __init__(self):

When put in the same directory, python test.py runs without error.

However, if I create a directory test1234 and put test1234.py and a blank init.py in this directory, python test.py gives the error:

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

What do I need to do for test.py to be able to see the test class in test1234.py?

1
  • 4
    Downvoters care to weigh in? Is my question not clear enough? Is it unacceptable that I didn't understand how python imports work? :) Commented Aug 14, 2012 at 11:45

1 Answer 1

2

You have to import it through the package, or put it in __init__.py.

import test1234.test1234
t = test1234.test1234.test()
Sign up to request clarification or add additional context in comments.

3 Comments

Put what, specifically, in __init__.py? I wish to only have import test1234 in test.py.
Nevermind, I see that you can do from test1234 import test in __init__.py.
Be careful with that last one though; consider renaming either the package or the submodule.

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.