leela leela - 15 days ago 9
Python Question

python - Importing a file that is a symbolic link

If I have files x.py and y.py . And y.py is the link(symbolic or hard) of x.py .

If I import both the modules in my script. Will it import it once or it assumes both are different files and import it twice.

What it does exactly?


Python will import it twice.

A link is a file system concept. To the Python interpreter, x.py and y.py are two different modules.

$ echo print \"importing \" + __file__ > x.py
$ ln -s x.py y.py
$ python -c "import x; import y"
importing x.py
importing y.py
$ python -c "import x; import y"
importing x.pyc
importing y.pyc
$ ls -F *.py *.pyc
x.py  x.pyc  y.py@  y.pyc