This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: In considering the current directory for importing modules, Python does not honour the output of os.getcwd()
Type: behavior Stage: resolved
Components: Extension Modules Versions:
process
Status: closed Resolution: duplicate
Dependencies: Superseder: importing yields unexpected results when initial script is a symbolic link
View: 6386
Assigned To: Nosy List: jamadagni, r.david.murray
Priority: normal Keywords:

Created on 2013-05-26 16:29 by jamadagni, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
symlink-bug.tar.gz jamadagni, 2013-05-26 16:29 Tarball of directory tree illustrating the problem
Messages (2)
msg190098 - (view) Author: Shriramana Sharma (jamadagni) Date: 2013-05-26 16:29
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!
msg190104 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2013-05-26 18:05
This is a duplicate of issue 6386, which does contain an explanation of the behavior.
History
Date User Action Args
2022-04-11 14:57:46adminsetgithub: 62267
2013-05-26 18:05:13r.david.murraysetstatus: open -> closed

superseder: importing yields unexpected results when initial script is a symbolic link
type: behavior

nosy: + r.david.murray
messages: + msg190104
resolution: duplicate
stage: resolved
2013-05-26 16:29:11jamadagnicreate