[Python-Dev] package imports, sys.path and os.chdir()

Steven D'Aprano steve at pearwood.info
Sun Apr 29 05:20:10 CEST 2012


Brett Cannon wrote:
> On Sat, Apr 28, 2012 at 04:08, Nick Coghlan <ncoghlan at gmail.com> wrote:
> 
>> On Sat, Apr 28, 2012 at 6:00 AM, Brett Cannon <brett at python.org> wrote:
>>> I'm personally in favour of changing the insertion of '' to sys.path to
>>> inserting the cwd when the interpreter is launched.
>> I'm not, because it breaks importing from the interactive prompt if
>> you change directory after starting the session.
>>
>>
> Who does that?

Me.

You're asking this as if it were a bizarre and disturbing thing to do. It's 
not as if changing directory is an unsupported hack.

When I use the Python interactive interpreter for interactive exploration or 
testing, sometimes I discover I'm in the wrong directory. If I've just started 
a fresh session, I'll probably just exit back to the shell, cd, then start 
Python again. But if there's significant history in the current session, I'll 
just change directories and continue on.


> I mean what possible need do you have to start the
> interpreter in one directory, but then need to chdir somewhere else where
> you are doing your actual importing from, and in a way where you can't
> simply attach the directory you want to use into sys.path?

Of course I could manipulate sys.path. But chances are that I still have to 
change directory anyway, so that reading and writing data files go where I 
want without having to specify absolute paths.



-- 
Steven



More information about the Python-Dev mailing list