'Why does Python follow symlinks? How do I work around that?
This is my directory layout:
projects/
+-projectA/
|---moduleA.py
|---script.py -> ../projectB/script.py
+-projectB/
|---script.py
This is what script.py contains:
import moduleA
print "hello there"
Here's what I'm doing:
$ pwd
[...]/projects/projectA
$ python2 script.py
Traceback (most recent call last):
File "script.py", line 1, in <module>
import moduleA
ImportError: No module named moduleA
The reason for this layout is that project A and project B are both from git repositories and get updated every once in a while. However, I write projectB and want to edit and push it, while not having to copy files over (it relies on stuff in project A).
Having it as a submodule/part of projectA is not an option, since the script has dependencies the maintainer of project A doesn't want.
The reverse is also not an option, since it's also so small in relation that it doesn't warrant adding project A (which is large and actually many different modules) as a submodule in project B, which would also create unnecessary extra files. Additionally, users are mainly be in project A's directory so a submodule would not make sense anyway.
I also don't want to use hard links for obvious reasons. I also can't do something like
sys.path.append("../projectA")
in script.py, because other users of script.py probably don't have the same directory layout as me, and some will download it straight to projectA (without cloning).
So first of all, why does the python interpreter follow that symlink, and not execute the code as if it was in projectA's directory?
And secondly, how can I solve this problem with the caveats stated above?
(Feel free to also just answer a single of the two above questions.)
Solution 1:[1]
I just run into the same "surprise" today with python3.9 -- so at least python ecosystem seems to be stable and upward compatible. ;)
For another surprise, using PYTHONPATH=. "fixes" that behaviour!
I would have guessed that only PYTHONPATH=$PWD would do the job, no idea why PYTHONPATH=. is fine too. ;)
$ python3.9 symlink.py # this breaks with "import local_module" -- ModuleNotFoundError: No module named 'local_module'
$ PYTHONPATH=. python3.9 symlink.py # works
$ PYTHONPATH=$PWD python3.9 symlink.py # works
I still do not like that "solution" (not yet?), so right now that work/test directory now doesn't use any symlinks anymore -- all stuff now gets copied/rsynced into the test dir. :-(
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | ouflak |
