[Python-ideas] Syntax to import modules before running command from the command line

Steve Barnes gadgetsteve at live.co.uk
Fri Jan 5 01:28:36 EST 2018

Currently invoking `python -c "some;separated;set of commands;"` will, 
if you need to use any library functions, require one or more import 
somelib; sections in the execution string. This results in rather 
complex "one liners".

On the other hand `python -m somelib` will load somelib and attempt to 
execute its `__main__()` or give an error if there isn't one.

What I would like to suggest is a mechanism to pre-load libraries before 
evaluating the -c option as this would allow the use of code from 
libraries that don't have a `__main__` function, or those that do but it 
doesn't do what you want.

Since -m for module is already taken I would suggest one of:
    -p for pre-load module
    -M for load module without attempting to execute `module.__main__()` 
and without defining "__main__" in the load context or
    -l for library
with the last two having the advantage of appearing next to -m in the 
--help output.

This would change, (for a trivial example):
    `python -c"import numpy;print(numpy.pi);"`
    `python -M numpy -c"print(numpy.pi);"`

Steve (Gadget) Barnes
Any opinions in this message are my personal opinions and do not reflect 
those of my employer.

More information about the Python-ideas mailing list