Why doesn't Python remember the initial directory?

Grant Edwards invalid at invalid.invalid
Mon Aug 20 12:28:59 EDT 2012


On 2012-08-20, kj <no.email at please.post> wrote:
> In <roy-CA6D77.17031119082012 at news.panix.com> Roy Smith <roy at panix.com> writes:
>
>>In article <k0rj38$2gc$1 at reader1.panix.com>, kj <no.email at please.post> 
>>wrote:
>
>>> As far as I've been able to determine, Python does not remember
>>> (immutably, that is) the working directory at the program's start-up,
>>> or, if it does, it does not officially expose this information.
>
>>Why would you expect that it would?  What would it (or you) do with this 
>>information?
>
> This means that no library code can ever count on, for example,
> being able to reliably find the path to the file that contains the
> definition of __main__.

What makes you think that the file that contains the definition of
__main__ is the working directory on program startup?  That's almost
never the case in my experience.

2) Why should a library expect to be able to access the file
   containing the definition of __main__.  

> That's a weakness, IMO.

You must be in possession of some rather odd use cases.  I've been
writing Python programs for something like 13 years, and I've never
felt any need to have either an immutable record of the startup
directory or access to the file containing the definition of __main__.

> I don't know of any way to fix inspect.getmodule that does not
> involve, directly or indirectly, keeping a stable record of the
> starting directory.

If what you really want is access to the definition of __main__, what
does that have to do with the startup directory?

If you want to know where __main__ is, you can probably figure it out
from /proc/self/<something-or-other>


-- 
Grant Edwards               grant.b.edwards        Yow! Am I having fun yet?
                                  at               
                              gmail.com            



More information about the Python-list mailing list