2

I have folder with such structure:

parent/
   ---__init__.py
   ---SomeClass.py
   ---Worker.py

First file (__init__.py) is empty.

Second file (SomeClass.py) content is following code:

class Test:
   pass

Third file (Worker.py):

import SomeClass
Test()

ImportError: No module named SomeClass

What I do wrong?

5
  • 1
    For me it fails in a different way - it fails to resolve Test() symbol. And that's fine - you need to either use the fully qualified name 'SomeClass.Test()', or import it into the module's name space: 'from SomeClass import Test' and left the second line as is. This doesn't answer your original question, however. How are you spawning this code? Commented May 24, 2011 at 11:12
  • Where are you running the program from, and how (e.g. import parent.Worker)? Commented May 24, 2011 at 11:17
  • @sam-magura running from same directory. Commented May 24, 2011 at 11:36
  • you mean python Worker.py from within 'parent'? Or importing it from the directory one-level up, as Sam Magura suggested? Give us more details :) Commented May 24, 2011 at 11:38
  • @CodePainters, I think it is due to the version of python Commented May 24, 2011 at 14:17

1 Answer 1

1

Try

from . import SomeClass

but remember you'll have to

SomeClass.Text()

instead of just Test()

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

3 Comments

Well I messed up, you can use it in 2.x too.
relative imports are Python 2.6 and later
Ah, yes I only checked 2.7. Using 3.x really makes hard dealing with 2.x issues. Well that was my reason to chose 3 =)

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.