Allow relative imports in the `__main__` module
还没有人认领这个 Issue。
- 主要语言
- Python
- 星标
- 77.2k
- 派生
- 35.9k
- PR 合并指标
- PR 指标待抓取
描述
Feature or enhancement
Proposal:
Expanding on https://github.com/python/cpython/issues/109853#issuecomment-2076845977
@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)
- Running a package module via
python -m <module>- Relative imports already work here, no changes needed.
- Running a source or extension module via
python -m <module>- Relative imports don't work here.
- Allowing relative imports here feels a bit weird, especially if we were to enable
-Pby 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.
- Running a package module directly (
python <module.zip>)- Relative imports already work here, no changes needed.
- Running a file directly (
python <script.py>)- Relative imports don't work here.
- Running a command string via
python -c <command>- Relative imports don't work here.
- There's no canonical base directory to use as a reference, should we use the current directory?
- Running a command string via the
stdin- Relative imports don't work here.
- There's no canonical base directory to use as a reference, should we use the current directory?
- Running the repl
- Relative imports don't work here.
- There's no canonical base directory to use as a reference, should we use the current directory?
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
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
调研方向
首先阅读 issue 109853 中链接的讨论,并比较这里列出的七种 main 执行场景,尤其是直接文件、-c、stdin 和 REPL。确定哪些情况应支持相对导入;在不存在规范基准时,应将哪个目录作为参考;以及将当前目录添加到 sys.path 是否能改善行为。在实现之前就支持范围和由此产生的用户体验达成一致,即视为完成。
由索引模型根据 Issue 内容生成。
评估
- 技术栈
- python
- 领域
- backend
- Issue 类型
- 功能
- 难度
- 5/5
- 预计耗时
- 一周以上
- 活跃度
- 停滞
- 描述清晰度
- 需要澄清
- 新手友好度
- 25/100