Import and absolute file names, sys.path including ''... or not
mk.fraggod at gmail.com
Thu May 21 02:08:04 CEST 2009
On Wed, 20 May 2009 22:01:50 +0200
Jean-Michel Pichavant <jeanmichel at sequans.com> wrote:
> You are right, but my concern is not the relative path resolution. Let
> me clarify:
> "import sys
> print sys.path"
> >python.exe test.py
> sys.path = ['/home/jeanmichel']
> > from within a python shell:
> sys.path = ['']
> The unpredictable effect of '' (at least something I did not predict) is
> that it allows absolute path resolution, while '/home/jeanmichel' cannot.
> Example :
> write a anotherTest.py file:
It works for me with py2.6, what version do you have?
> anotherTest.py will be successfully imported in a python shell ('' +
> '/home/jeanmichel/test.py' is a valid path), but the "python.exe
> anotherTest2.py" form will fail as it will try for '/home/jeanmichel'
> +'/home/jeanmichel/test.py' which is not a valid path.
I believe python uses os.path.join algorithm to combine paths which
discards anything (absolute or not) if absolute path gets appended to
os.path.join('/some/path', '/home/jeanmichel') == '/home/jeanmichel'
> So my question is: "why the shell is adding '' when the interpreter is
> adding the full path ?"
Looks like a solid way to construct relative imports to me.
Mike Kazantsev // fraggod.net
-------------- next part --------------
A non-text attachment was scrubbed...
Size: 205 bytes
Desc: not available
More information about the Python-list