Import and absolute file names, sys.path including ''... or not

Mike Kazantsev 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:
> 
> /home/jeanmichel/test.py:
> "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:
> "
> __import__('/home/jeanmichel/test')
> "

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
it:
  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...
Name: signature.asc
Type: application/pgp-signature
Size: 205 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/python-list/attachments/20090521/3d38f926/attachment.sig>


More information about the Python-list mailing list