Is module __file__ attribute absolute or relative?
I'm having trouble understanding __file__
. From what I understand, __file__
returns the absolute path from which the module was loaded.
I'm having problem producing this: I have a abc.py
with one statement print __file__
, running from /d/projects/
python abc.py
returns abc.py
. running from /d/
returns projects/abc.py
. Any reasons why?
Solution 1:
From the documentation:
__file__
is the pathname of the file from which the module was loaded, if it was loaded from a file. The__file__
attribute is not present for C modules that are statically linked into the interpreter; for extension modules loaded dynamically from a shared library, it is the pathname of the shared library file.
From the mailing list thread linked by @kindall in a comment to the question:
I haven't tried to repro this particular example, but the reason is that we don't want to have to call getpwd() on every import nor do we want to have some kind of in-process variable to cache the current directory. (getpwd() is relatively slow and can sometimes fail outright, and trying to cache it has a certain risk of being wrong.)
What we do instead, is code in site.py that walks over the elements of sys.path and turns them into absolute paths. However this code runs before '' is inserted in the front of sys.path, so that the initial value of sys.path is ''.
For the rest of this, consider sys.path
not to include ''
.
So, if you are outside the part of sys.path
that contains the module, you'll get an absolute path. If you are inside the part of sys.path
that contains the module, you'll get a relative path.
If you load a module in the current directory, and the current directory isn't in sys.path
, you'll get an absolute path.
If you load a module in the current directory, and the current directory is in sys.path
, you'll get a relative path.
Solution 2:
__file__
is absolute since Python 3.4, except when executing a script directly using a relative path:
Module
__file__
attributes (and related values) should now always contain absolute paths by default, with the sole exception of__main__.__file__
when a script has been executed directly using a relative path. (Contributed by Brett Cannon in bpo-18416.)
Not sure if it resolves symlinks though.
Example of passing a relative path:
$ python script.py
Solution 3:
Late simple example:
from os import path, getcwd, chdir
def print_my_path():
print('cwd: {}'.format(getcwd()))
print('__file__:{}'.format(__file__))
print('abspath: {}'.format(path.abspath(__file__)))
print_my_path()
chdir('..')
print_my_path()
Under Python-2.*, the second call incorrectly determines the path.abspath(__file__)
based on the current directory:
cwd: C:\codes\py
__file__:cwd_mayhem.py
abspath: C:\codes\py\cwd_mayhem.py
cwd: C:\codes
__file__:cwd_mayhem.py
abspath: C:\codes\cwd_mayhem.py
As noted by @techtonik, in Python 3.4+, this will work fine since __file__
returns an absolute path.