I have small script to echo the sys.argv (used to debug inputs to argparse):
2303:~/mypy$ cat echo.py
import sys
print(sys.argv)
If I run it, with the python '-i' option, I am left in an interpreter session with sys available:
2303:~/mypy$ python3 -i echo.py foo bar
['echo.py', 'foo', 'bar']
>>> sys.argv
['echo.py', 'foo', 'bar']
>>>
<exit>
Or a simple script using argparse:
import argparse
p = argparse.ArgumentParser()
p.add_argument('-p')
p.add_argument('foo')
args = p.parse_args()
print(args)
Run with '-i':
2313:~/mypy$ python3 -i stack60625769.py -p foo bar
Namespace(foo='bar', p='foo')
>>> p
ArgumentParser(prog='stack60625769.py', usage=None, description=None, formatter_class=<class 'argparse.HelpFormatter'>, conflict_handler='error', add_help=True)
>>> args
Namespace(foo='bar', p='foo')
>>>
I usually work in an ipython session. From there I can %run a script:
2312:~/mypy$ inumpy3
Python 3.6.9 (default, Nov 7 2019, 10:44:02)
Type 'copyright', 'credits' or 'license' for more information
IPython 7.11.1 -- An enhanced Interactive Python. Type '?' for help.
In [1]: %run stack60625769.py -p foo bar
Namespace(foo='bar', p='foo')
In [2]: args
Out[2]: Namespace(foo='bar', p='foo')
In [3]: p
Out[3]: ArgumentParser(prog='stack60625769.py', usage=None, description=None, formatter_class=<class 'argparse.HelpFormatter'>, conflict_handler='error', add_help=True)
I could run that parser again, with a custom argv (not the one that started the ipython session:
In [9]: p.parse_args('foo -p bar'.split())
Out[9]: Namespace(foo='foo', p='bar')
%run has various namespace options. The default is to run the script in a new namespace, but update the interactive on with its values.