473,699 Members | 2,701 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

relative import broken?

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::

from . import test2

Python 2.5.1 under Win2000, cmd line execution,
produces as output::

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

Why?

Thanks,
Alan Isaac
Apr 28 '07
12 2733
"Alex Martelli" <al***@mac.comw rote in message
news:1h******** *************** ****@mac.com...
So what do you think the answer should be?
Well I'm clearly not seeing into the depths of this,
so I'm not going to propose anything. But to stick
close to my example, I am not clear why a script
when executed could not do imports relative to
__file__. This seems like natural behavior to me.

Thanks,
Alan Isaac
May 2 '07 #11
"Alex Martelli" <al***@mac.comw rote in message
news:1h******** *************** **@mac.com...
Very simply, PEP 328 explains:
"""
Relative Imports and __name__

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.
"""
To change my question somewhat, can you give me an example
where this behavior (when __name__ is '__main__') would
be useful for a script? (I.e., more useful than importing relative
to the directory holding the script, as indicated by __file__.)

Thanks,
Alan Isaac
May 3 '07 #12
On May 3, 10:08 am, "Alan Isaac" <ais...@america n.eduwrote:
"Alex Martelli" <a...@mac.comwr ote in message

news:1h******** *************** **@mac.com...
Very simply, PEP 328 explains:
"""
Relative Imports and __name__
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.
"""

To change my question somewhat, can you give me an example
where this behavior (when __name__ is '__main__') would
be useful for a script? (I.e., more useful than importing relative
to the directory holding the script, as indicated by __file__.)
Do you realize it's a different behaviour and it won't work for some
packages? One possible alternative is to assume empty parent
package and let from . import foo work but not
from .. import bar or any other upper levels. The package author
should also realize __init__.py will be ignored.

-- Leo

May 4 '07 #13

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

Similar topics

0
1582
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" />. This import statement should load /var/www/dynamo/xsl/xhtml/basic.xhtml.xsl as far as I know. The current document is /var/www/dynamo/xml/index.xml (.xml documents are handled by the PHP parser on my server). This is the error message I get:...
0
1458
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 classical import syntax becomes absolute import only, so that all imports are explicitly one or the other. You can only use relative import from within packages. Trying to import a top-level module relatively from within same directory, produces...
1
5692
by: Alexey Borzenkov | last post by:
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__":
0
1338
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/ __init__.py subpackage1/
0
1395
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 them. Opening the tutorial I found following notice ( 6.4.2 ): "Note that both explicit and implicit relative imports are based on the name of the current module. Since the name of the main module is always "__main__", modules
10
194
by: test | last post by:
basic noob question here. i am trying to reference a package, i have the structure: mypack/ __init__.py test.py subdir1/ __init__.py mod1.py
4
1828
by: DG | last post by:
Alright, I have searched and searched and read many conversations on the topic of relative and absolute imports and am still not getting the whole thing through my skull. Highlights of what I've read: http://mail.python.org/pipermail/python-list/2007-January/422973.html http://groups.google.com/group/comp.lang.python/browse_thread/thread/b1e8dc93471a7079/8751c82cfe1ca3f2?lnk=gst&q=absolute+import#8751c82cfe1ca3f2...
0
1210
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 global function, that should import a dictionary from path deeper than the module itself. The import is done in the global function. In that global function, I get the modules path by
0
1500
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 me. Yes.
0
8686
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8615
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9033
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8911
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8882
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7748
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4375
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3057
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2345
muto222
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.