472,958 Members | 2,363 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Fun with relative imports and py3k

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
intended for use as the main module
of a Python application should always use absolute imports."

The distinction between modules and scripts was new to me. One of the
primary features I always appreciated in Python was that modules had a
dual purpose: they were components and they were programs. One could
use them in isolation or as a part of a package something I always
missed when I programmed in languages with the tyranny of a single
main.

However 'implicit relative imports' work as usual and Py3K just
changes the priority of search - one might simply ignore the
'explicit' variant and care for unambiguous names.

Now things are not so easy in life and when you are confronted with
other peoples software you have to know at least the rules of the
game.

Digging a little deeper I found the following section in "What's new
in Python 2.6"

'Python’s -m switch allows running a module as a script.
When you ran a module that was located inside a package, relative
imports didn’t work correctly.'

Hmm... they worked as specified or at least as documented. I admit I'm
confused but I suspect I'm not the only one. Now everything is fine.
You just run each and every module with the -m option "as a script"
because there is a chance that somewhere is a relative import ( e.g.
local to a function ) and suddenly the module fails to import stuff
due to a modality in the import behavior.

Let's see how it works in practice.

This here my very first attempt to start Pythons 2to3 refactoring tool
written by the master himself. I looked at it and he used relative
imports.

C:\Python30\Lib\lib2to3>C:\Python30\python -m refactor.py
Traceback (most recent call last):
File "C:\Python30\lib\runpy.py", line 103, in _run_module_as_main
loader, code, fname = _get_module_details(mod_name)
File "C:\Python30\lib\runpy.py", line 78, in _get_module_details
loader = get_loader(mod_name)
File "C:\Python30\lib\pkgutil.py", line 456, in get_loader
return find_loader(fullname)
File "C:\Python30\lib\pkgutil.py", line 466, in find_loader
for importer in iter_importers(fullname):
File "C:\Python30\lib\pkgutil.py", line 422, in iter_importers
__import__(pkg)
File "C:\Python30\lib\lib2to3\refactor.py", line 23, in <module>
from .pgen2 import driver
ValueError: Attempted relative import in non-package
Not working. I'm perfectly sure I don't understand the error message
but what's worse is that runpy.py fails which
was just created ( in Python 2.6 ) to fix the issue with the
apparently broken relative imports that is o.k. according to the
introductory material that is dedicated to newbies.

I'm trying to fix this naively by turning the explicit into implicit
relative (or absolute?) imports and see what happens.

refactor.py
------------------------
...

# Local imports
from pgen2 import driver
from pgen2 import tokenize

import pytree
import patcomp
import fixes
import pygram
....
C:\Python30\Lib\lib2to3>C:\Python30\python -m refactor.py
Traceback (most recent call last):
File "C:\Python30\lib\runpy.py", line 103, in _run_module_as_main
loader, code, fname = _get_module_details(mod_name)
File "C:\Python30\lib\runpy.py", line 78, in _get_module_details
loader = get_loader(mod_name)
File "C:\Python30\lib\pkgutil.py", line 456, in get_loader
return find_loader(fullname)
File "C:\Python30\lib\pkgutil.py", line 466, in find_loader
for importer in iter_importers(fullname):
File "C:\Python30\lib\pkgutil.py", line 422, in iter_importers
__import__(pkg)
File "refactor.py", line 27, in <module>
import patcomp
File "C:\Python30\lib\lib2to3\patcomp.py", line 17, in <module>
from .pgen2 import driver
ValueError: Attempted relative import in non-package

This time patcomp fails with its relative imports despite the fact
that it is not imported as a "script" but as a module inside the
lib2to3 package and it is not __main__ but refactor.py is.

I give up. I always thought migration from Python 2.X to Python 3.0
was an issue not the conversion tool ;)
Jun 27 '08 #1
0 1349

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: Randall Smith | last post by:
I've noticed the push by Guido and others to use absolute imports instead of relative imports. I've always enjoyed the ease of relative imports, but am starting to understand that "explicit is...
2
by: LesleyW | last post by:
I'm using a public set of schemas for GML, avaiable at www.opengis.net/gml. They all include or import each other but live in a folder hierarchy, so the import statement includes...
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...
1
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__ ==...
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::
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...
4
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...
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...
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

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.