1

I have a file I.py and X.py both are custom modules

I'm trying to do an import from X.py file like:

from myapp.I import Int

But, when I try to run in shell, it throws an import error of:

ImportError: No module named myapp.I

X.py is in a subdir of I.py

X.py is in: myapp/a/x.py
I.py is in: myapp/

what am I doing wrong? Sorry this is slightly basic, it seems so trivial, but I can't seem to get it to work.

Thank you.

4
  • Could you show us your directory structure? Commented May 13, 2014 at 17:55
  • Added above in a comment Commented May 13, 2014 at 17:57
  • from myapp.a.x import I Commented May 13, 2014 at 17:58
  • Im trying to import from within X.py, not I.py Commented May 13, 2014 at 18:02

2 Answers 2

2

You're going to need an __init__.py within myapp/ and myapp/a/. See What is __init__.py for? for more details.

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

2 Comments

If am inheriting I.py into X.py, doesn't this come too?
Without __init__.py, import operations won't be able to recognize a directory in Python 2.
0

You can't execute X.py directly, even with the required __init__.py files (have you added those?)

This is a limitation (or feature, depending on how you see it). To get around it, you need to create a simple runner script of sorts in the top-level directory which then imports X and executes it.

Comments

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.