New submission from Erik Carstensen <mandolae...@gmail.com>:

I can find partial information on how Python treats __main__.py here: 
https://docs.python.org/3/library/__main__.html

However, it is not documented how python handles __main__.py when passing the 
Python package to the interpreter without -m. If I have a program defined by 
/tmp/foo/bar.py and a file /tmp/foo/__main__.py, and I run

python /tmp/foo

... then Python will run __main__.py, with /tmp/foo prepended to sys.path.

I find this behaviour unfortunate; to me it would make more sense to prepend 
/tmp to sys.path, because then "python /tmp/foo" would be equivalent to 
"PYTHONPATH=/tmp python -m foo". With the current behaviour, if __main__.py 
wants to import bar.py, then it must write 'import bar' or 'import foo.bar' 
depending on whether the interpreter was invoked with -m.

Unfortunately, by Hyrum's Law, you can find people describing and encouraging 
reliance upon the current undocumented behaviour, e.g.:
https://www.geeksforgeeks.org/usage-of-__main__-py-in-python/
so perhaps the behaviour can't be changed that easily. Therefore, my request is 
to document how it works.

----------
assignee: docs@python
components: Documentation
messages: 396865
nosy: docs@python, mandolaerik
priority: normal
severity: normal
status: open
title: incomplete documentation of __main__.py
type: behavior
versions: Python 3.10, Python 3.11, Python 3.9

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue44552>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to