473,471 Members | 1,896 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Relative import bug or not?

After reading PEP-0328 I wanted to give relative imports a try:

# somepkg/__init__.py
<empty>

# somepkg/test1.py
from __future__ import absolute_import
from . import test2

if __name__ == "__main__":
print "Test"

# somepkg/test2.py
<empty>

But it complaints:
C:\1\somepkg>test1.py
Traceback (most recent call last):
File "C:\1\somepkg\test1.py", line 1, in <module>
from . import test2
ValueError: Attempted relative import in non-package

Does this mean that packages that implement self tests are not allowed
to use relative import? Or is it just a bug? I can understand that I
can use "import test2" when it's __main__, but when it's not now it
complains about no module test2 with absolute_import on.

PEP-0328 also has this phrase: "Relative imports use a module's
__name__ attribute to determine that module's position in the package
hierarchy. If the module's name does not contain any package
information (e.g. it is set to '__main__') then relative imports are
resolved as if the module were a top level module, regardless of where
the module is actually located on the file system.", but maybe my
english knowledge is not really good, because I can't understand what
should actually happen here ("relative imports are resolved as if the
module were a top level module")... :-/

So is it a bug, or am I doing something wrong?

Oct 14 '06 #1
1 5670

Alexey Borzenkov wrote:
After reading PEP-0328 I wanted to give relative imports a try:

# somepkg/__init__.py
<empty>

# somepkg/test1.py
from __future__ import absolute_import
from . import test2

if __name__ == "__main__":
print "Test"

# somepkg/test2.py
<empty>

But it complaints:
C:\1\somepkg>test1.py
Traceback (most recent call last):
File "C:\1\somepkg\test1.py", line 1, in <module>
from . import test2
ValueError: Attempted relative import in non-package

Does this mean that packages that implement self tests are not allowed
to use relative import? Or is it just a bug? I can understand that I
can use "import test2" when it's __main__, but when it's not now it
complains about no module test2 with absolute_import on.

PEP-0328 also has this phrase: "Relative imports use a module's
__name__ attribute to determine that module's position in the package
hierarchy. If the module's name does not contain any package
information (e.g. it is set to '__main__') then relative imports are
resolved as if the module were a top level module, regardless of where
the module is actually located on the file system.", but maybe my
english knowledge is not really good, because I can't understand what
should actually happen here ("relative imports are resolved as if the
module were a top level module")... :-/

So is it a bug, or am I doing something wrong?

Short version is: relative imports do not work in 2.5 when a script is
run as "__main__"

Jürgen

Oct 15 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Rutger Claes | last post by:
In the directory /var/www/dynamo/xsl is the basic xsl file for xhtml documents /var/www/dynamo/xsl/xhtml.xsl. In this file there is an import statement: <xsl:import href="xhtml/basic.xhtml.xsl"...
0
by: Anders J. Munch | last post by:
Now 2.5 is out, and we have a syntax for explicit relative imports (PEP 328, http://www.python.org/dev/peps/pep-0328/, in case anyone wasn't paying attention). The long-term plan is that the...
12
by: Alan Isaac | last post by:
Are relative imports broken in 2.5? Directory ``temp`` contains:: __init__.py test1.py test2.py File contents: __init__.py and test2.py are empty test1.py contains a single line::
0
by: Alan Isaac | last post by:
What is the recommended packaging of demo scripts or test scripts for a package that has modules that use relative imports? Example: Suppose I have the package structure: package/...
9
by: rbygscrsepda | last post by:
Hi, I'm a newbie at Python. :) Right now it's not letting me import * from any relative package name--i.e., a name that starts with a dot. For instance, none of the following work: from . import...
0
by: Kay Schluehr | last post by:
Since their introduction in Python 2.5 I only reviewed the new "relative import" notation briefly by reading the "What's new in Python 2.5" article. Now I wanted checkout if I get comfortable with...
7
by: Robert Hancock | last post by:
mypackage/ __init__.py push/ __init__.py dest.py feed/ __init__py subject.py In subject.py I have
0
by: Stef Mientki | last post by:
hello, I'm running Python 2.5 and want my programs to run at least under Windows and Linux (preferable also Mac). So I guess I should always use relative paths. From most modules I can call a...
0
by: Gabriel Genellina | last post by:
En Sat, 18 Oct 2008 05:52:04 -0300, Stef Mientki <stef.mientki@gmail.com> escribió: Why don't you let the caller tell you its own location, using __file__? The above code is too much magic for...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.