Allow relative imports in the __main__
module
#126926
Labels
type-feature
A feature request or enhancement
__main__
module
#126926
Feature or enhancement
Proposal:
Expanding on #109853 (comment)
@zooba, were you envisioning to do this for all scenarios where a
__main__
module is exists? I think that makes sense for consistency, but we may want to consider all scenarios to make sure the UX makes sense.AFAICT, these should be all of them:
(this refers to relative imports in the same directory, it does not cover relative imports to the parent directory)
python -m <module>
python -m <module>
-P
by default. For it to run, the module would have to be insys.path
, meaning it probably installed rather than being provided by the user, so there isn't much necessity for relative imports.python <module.zip>
)python <script.py>
)python -c <command>
stdin
I think 4), 5), 6), and 7) are the only use-cases that call for relative imports to work. 2) feels a bit weird, but it may be worth supporting for consistency.
I played a bit with the code, and the implementing this doesn't seem much difficult. I was able to write a working PoC targeting only 4) with much difficulty, so I don't think that's a worry.
Considering this, do we think it makes sense to make this change?
If so, should it cover all scenarios, or do we want to keep some of them as-is?
For further reflection, how much of an actual improvement would this actually be over the current behavior of adding the current directory to
sys.path
?I can't help but feel a bit like we are trading one weird behavior for another.
Has this already been discussed elsewhere?
No response given
Links to previous discussion of this feature:
No response
The text was updated successfully, but these errors were encountered: