New submission from Shriramana Sharma:

Hello. I first asked about this at 
https://groups.google.com/d/topic/comp.lang.python/ZOGwXGU_TV0/discussion and 
am only posting this issue due to no reply there.

I am using Python 2.7.4 and Python 3.3.1 (default packages) on Kubuntu Raring. 
On both I experience this same bug.

This bug has to do with a Python script called via a symlink. To illustrate it 
I have included a minimal test case tarball as an attachment. Just run sh 
test.sh at root of the extracted tree to see what's happening.

I am quite unpleasantly surprised that when one calls a Python script via a 
symlink, and that script asks for a module to be imported, Python searches the 
directory in which the *target* of the link exists, and uses the version of the 
library present *there* if any, and raises an exception if not. It even follows 
a chain of symlinks. Any version of the library present in the same directory 
as the (user-called) symlink is ignored. This is totally counter-intuitive 
behaviour and should be treated as a bug and fixed.

This is all the more frustrating since running print(os.getcwd()) from the same 
script correctly prints the current directory in which the *symlink* and not 
its target exists. (See output of the attached scripts.)

Now the symlink is only a user-level file system convenience indicating that I 
create a virtual file in one place pointing to another file elsewhere. Whatever 
the rest of the contents of the directory containing the other file is 
immaterial to me -- I am only interested in the one file I am symlinking to. 

I am executing a script from a given directory. os.getcwd() correctly prints 
the path of that directory. I also have a library in that same directory for 
the script to import. I would expect Python to honour the output of os.getcwd() 
in doing import too.

I read through http://docs.python.org/3/reference/import.html and didn't seem 
to find any explanation for the current illogical behaviour. (Please point out 
if I have missed it.)

(Note that the same behaviour does not happen with hardlinks, probably since 
the filesystem itself shows the whole file as existing at the current location.)

Output of the test.sh script:

**** Trying english/run.py ****
CWD: /tmp/symlink-bug/english
Hello Shriramana!
**** Trying english/run-link.py symlinked to ./run.py ****
CWD: /tmp/symlink-bug/english
Traceback (most recent call last):
  File "run-link.py", line 3, in <module>
    from greet import greet
ImportError: No module named greet
**** Trying english/run-link-link.py symlinked to ./run-link.py symlinked to 
english/run.py ****
CWD: /tmp/symlink-bug/english
Hello Shriramana!
**** Trying sanskrit/run-slink.py symlinked to english/run.py ****
CWD: /tmp/symlink-bug/sanskrit
Hello Shriramana!
**** Trying sanskrit/run-hlink.py hardlinked to english/run.py ****
CWD: /tmp/symlink-bug/sanskrit
Namaste Shriramana!

Expected output: (see esp items marked 1 and 2 below):

**** Trying english/run.py ****
CWD: /tmp/symlink-bug/english
Hello Shriramana!
1 **** Trying english/run-link.py symlinked to ./run.py ****
CWD: /tmp/symlink-bug/english
Hello Shriramana!
**** Trying english/run-link-link.py symlinked to ./run-link.py symlinked to 
english/run.py ****
CWD: /tmp/symlink-bug/english
Hello Shriramana!
2 **** Trying sanskrit/run-slink.py symlinked to english/run.py ****
CWD: /tmp/symlink-bug/sanskrit
Namaste Shriramana!
**** Trying sanskrit/run-hlink.py hardlinked to english/run.py ****
CWD: /tmp/symlink-bug/sanskrit
Namaste Shriramana!

----------
components: Extension Modules
files: symlink-bug.tar.gz
messages: 190098
nosy: jamadagni
priority: normal
severity: normal
status: open
title: In considering the current directory for importing modules, Python does 
not honour the output of os.getcwd()
Added file: http://bugs.python.org/file30386/symlink-bug.tar.gz

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue18067>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to