Import 'filename'? (was Re: Cross-reference 'import' in a classhierarchy)

Chris Reedy creedy at mitretek.org
Wed Apr 9 16:05:28 CEST 2003


Greg Ewing (using news.cis.dfn.de) wrote:
> Chris Reedy wrote:
> 
>> I had to think about this one for a while. Here's the issue I see:
>>
>> What exactly does import '../foo/ook.py' actual do?
> 
> 
> You're being distracted by a red herring, I think. What
> I took the original post to mean was doing the equivalent
> thing in the Python module namespace, not the file
> system namespace.

You may be right. Thinking about this in the module namespace makes this 
seem better.

> Some suitable syntax would need to be devised,
> perhaps
> 
>   import ..foo.ook
> 
> although I'm not sure how you would refer to the
> imported module after that... maybe an "as" clause
> should be mandatory in this case?

There was a suggestion made in an old e-mail for something like __root__ 
or maybe __parent__ meaning the parent module of this one. So you would 
say something like:

   import __root__.foo.ook

I think that would have to be backed up by globals, something like 
__root__ or __parent__ referring to the parent of this module and maybe 
__self__ (or __this__ or __module__ or ...) which would refer to the 
current module. This would allow you do to things like:

   import __root__.foo.ook
   __root__.foo.ook.f()

Otherwise, I think you're right, an "as" clause would be mandatory.

> If you want to work in the file system namespace, you
> can already do that:
> 
>   ook = __import__("../foo/ook.py")
> 
>> Based on some reading in the email archives, there is clearly a desire 
>> (maybe a mandate by the BDFL) that some sort of correspondence be 
>> guaranteed to exist between the above four definitions (especially 1, 
>> 2, and 3).
> 
> 
> I sometimes wonder whether the Python module namespace and
> the file system should be one and the same thing. I certainly
> believe that PYTHONPATH is a mistake -- the environment required
> for a program to function correctly is a property of the program
> itself, not of the user's personal settings.

I don't like the idea of working in the file system namespace. I 
observed a whole discussion on the GCC mailing list of when (in an os 
independent fashion) two files are "the same". I remember a discussion 
of looking at things like inodes. I don't remember any resolution. I 
don't find myself that unhappy with PYTHONPATH.

> Something better is needed, I'm just not sure exactly what
> yet.

Agreed.

-- 
This is informal and not an official Mitretek Systems position.
Dr. Christopher L. Reedy, Senior Principal Software Engineer
Mitretek Systems, 3150 Fairview Park Drive South, Falls Church, VA 
22042-4519
Email: creedy at mitretek.org  Phone: (703) 610-1615  FAX: (703) 610-2203



-----------== Posted via Newsfeed.Com - Uncensored Usenet News ==----------
   http://www.newsfeed.com       The #1 Newsgroup Service in the World!
-----= Over 100,000 Newsgroups - Unlimited Fast Downloads - 19 Servers =-----




More information about the Python-list mailing list