classification
Title: Module's __file__ should be absolute always ("." in sys.path)
Type: enhancement Stage: resolved
Components: Interpreter Core Versions: Python 3.7
process
Status: closed Resolution: rejected
Dependencies: Superseder:
Assigned To: Nosy List: BTaskaya, blueyed, brett.cannon, eric.snow, ncoghlan
Priority: normal Keywords:

Created on 2018-08-20 16:57 by blueyed, last changed 2020-01-02 22:12 by brett.cannon. This issue is now closed.

Messages (5)
msg323800 - (view) Author: daniel hahler (blueyed) * Date: 2018-08-20 16:57
With "." in sys.path the "__file__" attribute will be a relative path, and therefore cannot be used after "chdir".

This likely affects relative paths in general, but have not tested it.

```
import os
import sys

sys.path.insert(0, '.')

# Importing it before chdir already causes failure.
import imported

os.chdir('/')
print(imported.__file__)  # ./imported.py
assert imported.__file__ == os.path.abspath(imported.__file__)
```

It works for "" in sys.path (https://bugs.python.org/issue18416).
msg358777 - (view) Author: Batuhan Taskaya (BTaskaya) * (Python committer) Date: 2019-12-21 18:52
I am not sure about computing absolute path every time when `__file__` called. I guess it is a static thing, that get setted on the import time and never changes. What you are proposing is to compute it dynamically which IMHO isn't the best way.
msg358852 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2019-12-24 17:59
So if you were to insert '.' via PYTHONPATH it would be made absolute as the site module makes all entries in sys.path absolute during startup.

And changing this to have to check every time import runs if an entry in sys.path is absolute would be costly (that's not a insignificant number of stat calls which we always try to avoid during import when possible). Your best option is to insert an absolute path to begin with. Also realize that manipulating sys.path is an advanced thing to do and so I don't think asking people to be "consenting adults" and be careful about adding relative paths on sys.path is an unreasonable thing to ask.

So while I appreciate the report, Daniel, and the motivation, I'm closing this as "won't fix" as the overhead of implementing this is too high.
msg358855 - (view) Author: daniel hahler (blueyed) * Date: 2019-12-24 18:29
> And changing this to have to check every time import runs if an entry in sys.path is absolute would be costly (that's not a insignificant number of stat calls which we always try to avoid during import when possible).

This could be done when inserting something into `sys.path` maybe then only?
But might only make sense when there is some special handling in that case already though - and would change existing behavior of course.
(Therefore I am OK with rejecting this, but wanted to mention it anyway)

> Your best option is to insert an absolute path to begin with.

That's a good enough option to have anyway, of course.
msg359222 - (view) Author: Brett Cannon (brett.cannon) * (Python committer) Date: 2020-01-02 22:12
RE: "This could be done when inserting something into `sys.path` maybe then only?" That would require a custom object instead of a standard list for sys.path which could detect insertion and then do the automatic absolute path resolution which as you pointed out will inevitably break things for someone somewhere.
History
Date User Action Args
2020-01-02 22:12:00brett.cannonsetmessages: + msg359222
2019-12-24 18:29:34blueyedsetmessages: + msg358855
2019-12-24 17:59:52brett.cannonsetstatus: open -> closed
type: enhancement
messages: + msg358852

resolution: rejected
stage: resolved
2019-12-21 18:52:29BTaskayasetnosy: + eric.snow, brett.cannon, ncoghlan, BTaskaya
messages: + msg358777
2018-08-20 16:57:16blueyedcreate