When I run a script with Ipython in interactive mode, the sys.argv argument list is different in the interactive part of the execution than in the script.
Is this a bug, or am I doing something wrong?
Thanks!
oskar@RR06:~$ cat test.py
import sys
print(sys.argv)
temp = sys.argv
oskar@RR06:~$ ipython -i test.py -- foo bar
Python 2.7.6 (default, Jun 22 2015, 17:58:13)
Type "copyright", "credits" or "license" for more information.
IPython 4.2.1 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
['/home/oskar/test.py', 'foo', 'bar']
In [1]: temp
Out[1]: ['/home/oskar/test.py', 'foo', 'bar']
In [2]: sys.argv
Out[2]: ['/usr/local/bin/ipython', '-i', 'test.py', '--', 'foo', 'bar']
If I just invoke ipython, and look at sys.argv I get
In [3]: sys.argv
Out[3]: ['/usr/bin/ipython3']
Your Out[2] looks the same - the full list as provided by the shell and Python interpreter. Remember we are running a Python session with ipython import:
#!/usr/bin/env python3
# This script was automatically generated by setup.py
if __name__ == '__main__':
from IPython import start_ipython
start_ipython()
/usr/bin/ipython3 (END)
But look at ipython -h; in the first paragraph:
it executes the file and exits, passing the remaining arguments to the script, just as if you had specified the same command with python. You may need to specify
--before args to be passed to the script, to prevent IPython from attempting to parse them.
So it's explicitly saying that
ipython -i test.py -- foo bar
becomes (in effect) - or is run as:
python test.py foo bar
The ipython code has a parser (as subclass argparse) that handles many different arguments. But ones it can't handle, or follow -- are set aside, and put in the sys.argv that your test.py sees.
But apparently that sys.argv is not what is given to the interactive session.
I think you'd get the same effect
$ipython
In[0]: %run test.py foo bar
...
%run saves the current sys.argv, and constructs a new one with sys.argv = [filename] + args. Then after running your test.py it restores the sys.argv.
This is not a bug, and you aren't doing anything wrong - except expecting the two sys.argv to be the same. It appears that in a plain Python shell, the two sys.argv are the same (without any of the options that the shell itself uses).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With