473,387 Members | 1,790 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

A question about "Intra-package References"

ChrisWang
Hi all,

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

|-MyPkg
-__init__.py
-example.py
-test.py

example.py is like this:
Expand|Select|Wrap|Line Numbers
  1. a = 5
and test.py is like this:
Expand|Select|Wrap|Line Numbers
  1. from . import example
  2.  
  3. print example.a
While I executed test.py, python said "ValueError: Attempted relative import in non-package". Could anybody tell me what's wrong with the code. Thanks!
Dec 15 '08 #1
3 3157
bvdet
2,851 Expert Mod 2GB
My limited understanding is that you cannot use relative imports in a top-level module. There does not appear to be anything wrong.

-BV
Dec 15 '08 #2
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
bvdet
2,851 Expert Mod 2GB
You are welcome, and thank you for the feedback ChrisWang.
Dec 16 '08 #4

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

Similar topics

10
by: mike | last post by:
regards: I use Jtidy (api) to translate a HTML file into a "XHTML file". But The "XHTML file" cannot be identified by nokia 6600. Do I miss something important? Or this is Jtidy's weakness or...
32
by: Marcus | last post by:
We all know that the "gets" function from the Standard C Library (which is part of the Standard C++ Library) is dangerous. It provides no bounds check, so it's easy to overwrite memory when using...
0
by: Peter | last post by:
When I issue call sqlj.install_jar('file:///f:/jars/mail.jar','MAIL'); I get the messages SQL4301N Java or .NET interpreter startup or communication failed, reason
8
by: Martin | last post by:
I hope not, but, I think the answer to this question is "it can't be done". Northwind sample database. Orders form. Go to a new record. Select a customer in "Bill To:" Don't enter any...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.