473,386 Members | 1,864 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,386 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 1368

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...
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: 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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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.