By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,285 Members | 1,647 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,285 IT Pros & Developers. It's quick & easy.

A question about "Intra-package References"

P: 9
Hi all,

I have a simple question about "Intra-package References"
There are two source files and in package MyPkg shown below:

|-MyPkg is like this:
Expand|Select|Wrap|Line Numbers
  1. a = 5
and is like this:
Expand|Select|Wrap|Line Numbers
  1. from . import example
  3. print example.a
While I executed, python said "ValueError: Attempted relative import in non-package". Could anybody tell me what's wrong with the code. Thanks!
Dec 15 '08 #1
Share this Question
Share on Google+
3 Replies

Expert Mod 2.5K+
P: 2,851
My limited understanding is that you cannot use relative imports in a top-level module. There does not appear to be anything wrong.

Dec 15 '08 #2

P: 9
Thank you. I've got the answer why the error happened.

"Since the name of the main module is always "__main__", modules intended for use as the main module of a Python application should always use absolute imports."

So I made another main module with absolute imports, and also imports this test module. Everything is fine.

Thanks again for your help:)
Have a nice day!
Dec 16 '08 #3

Expert Mod 2.5K+
P: 2,851
You are welcome, and thank you for the feedback ChrisWang.
Dec 16 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.