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

Brett Cannon brett at python.org
Sat Apr 28 21:16:00 CEST 2012


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? 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?



> The existing workaround for applications is pretty trivial:
>
>  # Somewhere in your initialisation code
>  for i, entry in enumerate(sys.path):
>      sys.path[i] = os.path.abspath(i)
>
> The fix for the import system is similarly trivial: call
> os.path.abspath when calculating __file__ (just as runpy now does and
> the import emulation in pkgutil always has).
>

You say trivial, I say a pain as that means porting over os.path.abspath()
into importlib._bootstrap that works for all platforms.

-Brett


>
> Cheers,
> Nick.
>
> --
> Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20120428/c2fa3ee6/attachment.html>


More information about the Python-Dev mailing list