On Sat, Jan 11, 2020, at 02:06, Andrew Barnert via Python-ideas wrote:
> Well, there are definitely subtle differences. If it’s a single file 
> rather than a package, running it as a script means you have to include 
> the extension; with -m you can’t. If you -m a package, argv[0] is the 
> path to its __main__.py; if you run a package as a script it’s the path 
> to the package. If you -m the parent directory of the package obviously 
> has to be on sys.path, but if you run it as a script, it generally 
> won’t be. There might also be differences with which paths (argv[0], 
> __file__, __cached__, etc.) get abspathed on each platform (I can never 
> remember the rules for that anyway). Do they both work with namespace 
> packages? Fire the same auditing events? Interact the same way with the 
> Windows py.exe launcher and shbang lines? Does -m ignore case on *nix 
> on case-insensitive filesystems? What if you’ve got weird stuff in your 
> metapath from a site-installed import hook? I don’t know the answers 
> for most of these.

speaking of executing packages from the command line... I noticed a different 
set of anomalies (I was going to suggest this as a solution or workaround, but 
it turned out to be non-viable).

If you specify a directory name as the primary script argument to python 
*without* -m:
- __main__.py will be executed
- argv[0] will be the dir name as-is, not a path to __main__.py
- sys.path[0] is the given directory
- __package__ is *the empty string*. [under normal circumstances for a module 
not in a package, __package__ is None. However, the empty string here does not 
allow from . import to work]

I expected:
- __main__.py will be executed
- I had no particular expectations regarding argv[0], but I believe this turns 
out to be *only* circumstance under which it can name something other than a 
python file
- sys.path[0] to be the parent directory of the given directory
- __package__ to be the name of the directory

Also note that executing a zip file on the command line behaves similarly to 
the actual behavior described above, and there is no way to achieve behavior 
analogous to what I expected because there is no clear way to treat a zip file 
as a package. I have not considered the implications regarding zip files 
further.
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/SP6BK5P7QXIEU7LWUK4HQFBPQCR4I2MM/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to