By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,908 Members | 2,055 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,908 IT Pros & Developers. It's quick & easy.

Double symlinking breaks module import

P: n/a
Hello!

I use Python 2.2.2 on Linux. I met the following issue:

Say I have the module mymod and the script myprog.py in /mydir.
myprog.py imports mymod. If I create a symlink
/foo/myprog2.py -> /mydir/myprog.py, everything works as it should,
invoking any of /foo/myprog2.py /mydir/myprog.py work in the same way. But
if I create another symlink myprog3.py -> myprog2.py, and then I invoke
myprog3.py, I get compliances that mymod is not found.

Is it a bug or policy?

--
Csaba

Set theory is the LSD of mathematics
Jul 18 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Csaba Henk:
The symlink can't be wrong because the python program itself (to which the
symlinks refer) starts up; it just doesn't find the module. This question is related to importing modules, and the list of dirs in which the module
seeked is a Python internal, so it does not depend only on file functions.
How symlinking affect the module search path?


Ahh, I think I know what's causing this. Here's how sys.path gets
set, from the documentation for the sys module.

] As initialized upon program startup, the first item of this list, path[0],
is
] the directory containing the script that was used to invoke the Python
] interpreter. If the script directory is not available (e.g. if the
interpreter
] is invoked interactively or if the script is read from standard input),
] path[0] is the empty string, which directs Python to search modules
] in the current directory first. Notice that the script directory is
inserted
] before the entries inserted as a result of PYTHONPATH

%mkdir dir1
%cat > dir1/blah.py
import sys
print "path[0] ==", sys.path[0]

%mkdir dir2
%ln -s ../dir1/blah.py dir2
%cat dir2/blah.py
import sys
print "path[0] ==", sys.path[0]

%python dir1/blah.py
path[0] == dir1
%python dir2/blah.py
path[0] == dir2/../dir1
%
%ln -s dir2/blah.py .
%python blah.py
path[0] == dir2
%

So there's some interaction here between the OS and Python (the
details of which I don't know) that causes the problem you see.

If I had to hazard a guess, there's probably a use of lstat once
when it should be recursively.

Feel free to submit this as a bug.

Andrew
da***@dalkescientific.com
Jul 18 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.