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

Ivan Pozdeev vano at mail.mipt.ru
Tue Jan 9 16:56:56 EST 2018

On 10.01.2018 0:54, Barry Warsaw wrote:
> Steve Barnes wrote:
>> 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.
> It would be really cool if you could somehow write a file with a bunch
> of commands in it, and then get Python to execute those commands.  Then
> it could still be a one line invocation, but you could do much more
> complex things, including import a bunch of modules before executing
> some code.  I'm not sure what such a file would look like but here's a
> strawman:
IPython's `run -i' does that.
> ```
> import os
> import sys
> import somelib
> path = somelib.get_path()
> parent = os.path.dirname(path)
> print(parent)
> sys.exit(0 if os.path.isdir(parent) else 1)
> ```
> Then you could run it like so:
> $ python3 myscript.py
> That seems like a nice, compact, one line invocation, but cI don't know,
> it probably needs some fleshing out.  It's just a crazy idea, and
> there's probably not enough time to implement this for Python 3.7.
> Maybe for Python 3.8.
> time-machine-winking-ly y'rs,
> -Barry
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/


More information about the Python-ideas mailing list