tags:

views:

248

answers:

2

Hi,

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?

+3  A: 

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
codeape
A: 

You only have to be careful in the case where your script itself is a symbolic link, in which case the first entry of sys.path will be the directory containing the target of the link.

Neil