[issue20443] __code__. co_filename should always be an absolute path

STINNER Victor report at bugs.python.org
Thu Jun 27 20:18:23 EDT 2019


STINNER Victor <vstinner at redhat.com> added the comment:

Example of case where a module path is still relative:
---
import sys
import os
modname = 'relpath'
filename = modname + '.py'
sys.path.insert(0, os.curdir)
with open(filename, "w") as fp:
    print("import sys", file=fp)
    print("mod = sys.modules[__name__]", file=fp)
    print("print(f'{__file__=}')", file=fp)
    print("print(f'{mod.__file__=}')", file=fp)
    print("print(f'{mod.__cached__=}')", file=fp)
__import__(modname)
os.unlink(filename)
---

Output:
---
__file__='./relpath.py'
mod.__file__='./relpath.py'
mod.__cached__='./__pycache__/relpath.cpython-39.pyc'
---

__file__ and mod.__file__ are relative paths: not absolute paths.

----------

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue20443>
_______________________________________


More information about the Python-bugs-list mailing list