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

Christian Tismer tismer at stackless.com
Sun Apr 29 15:37:40 CEST 2012

On 29.04.12 07:05, Nick Coghlan wrote:
> On Sun, Apr 29, 2012 at 1:41 PM, PJ Eby<pje at telecommunity.com>  wrote:
>> That's already the case.  Actually, sys.path[0] is *always* the absolute
>> path of the script directory -- regardless of whether you invoked the script
>> by a relative path or an absolute one, and regardless of whether you're
>> importing 'site' -- at least on Linux and Cygwin and WIndows, for all Python
>> versions I've used regularly, and 3.2 besides.
> "-c" and "-m" also insert the empty string as sys.path[0] in order to
> find local files. They could just as easily insert the full cwd
> explicitly though, and, in fact, they arguably should. (I say
> arguably, because changing this *would* be a backwards incompatible
> change - there's no such issue with requiring __file__ to be
> absolute).

As a note: I tried to find out where and when the empty string actually
got inserted into sys.path. Not very easy, had to run the C debugger
to understand that:

It happens in sysmodule.c

     PySys_SetArgv(argc-_PyOS_optind, argv+_PyOS_optind);

that calls

PySys_SetArgvEx(int argc, char **argv, int updatepath)

and the logic weather to use the empty string or a full path etc.
is deeply hidden in a C function as a side effect. Brrrrrr!

It would be much cleaner and easier if that stuff would be ignored
today and called a Python implementation, instead.

Is that in the plans to get rid of C for such stuff? I hope so :-)

cheers -- Chris

Christian Tismer             :^)<mailto:tismer at stackless.com>
tismerysoft GmbH             :     Have a break! Take a ride on Python's
Karl-Liebknecht-Str. 121     :    *Starship* http://starship.python.net/
14482 Potsdam                :     PGP key ->  http://pgp.uni-mainz.de
work +49 173 24 18 776  mobile +49 173 24 18 776  fax n.a.
PGP 0x57F3BF04       9064 F4E1 D754 C2FF 1619  305B C09C 5A3B 57F3 BF04
       whom do you want to sponsor today?   http://www.stackless.com/

More information about the Python-Dev mailing list