relative import broken?

Sam landofdreams at gmail.com
Wed Apr 30 23:46:22 EDT 2008


On Apr 30, 9:11 pm, Hrvoje Niksic <hnik... at xemacs.org> wrote:
> Sam <landofdre... at gmail.com> writes:
> > I also have a problem with relative import; I can't for the life of me
> > figure out how to use the damn thing. I think the main problem is with
> > getting Python to recognize the existence of a package. I have
>
> > S/
> >   p.py
> >   B/
> >     b.py
> >   W/
> >     pyw/
> >       u.py
> >       ws.py
>
> > and I'd like to get u.py to import all the other 3 programs. I put
> > empty __init__.py files in all of the above directories (is this
> > necessary?), and even manually added the pathway (r'C:\Myname\S') to
> > sys.path, but when I execute
>
> > from S import p
>
> > in u.py Python gives "ImportError: No module named S".
>
> A silly question: is the directory that contains "S" in PYTHONPATH or
> in sys.path?

It's in sys.path. I'm not sure how to access or change PYTHONPATH from
within a program (I'm running everything in IDLE). I'm using Windows,
btw.

>
> > The docs for relative import make this sound much easier than it is.
>
> It's supposed to be just as easy as it sounds.  For example:
>
> $ mkdir S
> $ touch S/p.py
> $ touch S/__init__.py
> $ python
> Python 2.5.2 (r252:60911, Apr 21 2008, 11:12:42)
> [GCC 4.2.3 (Ubuntu 4.2.3-2ubuntu7)] on linux2
> Type "help", "copyright", "credits" or "license" for more information.>>> from S import p
> >>> p
>
> <module 'S.p' from 'S/p.py'>




More information about the Python-list mailing list