[Tutor] problem importing class

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Mon Oct 30 15:06:27 CET 2006


> This only works for code that is in a package, when you want to import a
> module in the same package. It doesn't work for code that is not in a
> package. For example,
>
> F:\Tutor>cat site.py
> print 'imported site.py'
>
> F:\Tutor>cat siteimporter.py
> from __future__ import absolute_import
> from . import site
>
> F:\Tutor>py siteimporter.py
>
> F:\Tutor>C:\Python25\python siteimporter.py
> Traceback (most recent call last):
>   File "siteimporter.py", line 2, in <module>
>     from . import site
> ValueError: Attempted relative import in non-package

Hi Kent,


Yikes!  Really?

Thank you for the correction; I completely misread the intention of PEP 
328 then.


But now I'm very miffed and unhappy.  Darn it, then that means this issue 
isn't repaired after all.  Most beginner programmers won't use packages, 
and so we'll still see the same problems pop up all the time.  Why is this 
restricted just to packages?  Let me see...

     http://mail.python.org/pipermail/python-list/2006-October/367267.html

Hmmm...  Is that the issue then?  It being the main module?  I'm now 
reading PEP 338 and looking at the section "Import Statements and the Main 
module":

     http://www.python.org/dev/peps/pep-0338/

... ok.  Good grief.  Then the current state of affairs still doesn't 
address the common case problem that we see on Tutor on module imports. 
The PEPs maintain that this is the situation for Python 2.5, emphasizing 
that this might be revisited in 2.6, which gives me some fleeting hope 
that this might get fixed in the future.

I apologize about giving the bad advice.  I was really hoping never to see 
another import problem again.


More information about the Tutor mailing list