Double symlinking breaks module import

C

Csaba Henk

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?
 
A

Andrew Dalke

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
(e-mail address removed)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,095
Messages
2,570,616
Members
47,232
Latest member
helpplease!

Latest Threads

Top