Spyder: Error when executing argparse in IPython console

Created on 23 Dec 2016  路  8Comments  路  Source: spyder-ide/spyder

The following code snippet fails when it is executed with F9 or in cell mode.

import argparse
parser = argparse.ArgumentParser()
args = parser.parse_args()

The reason is the arguments passed to the ipython console, in particular the -f:

>>> import sys
>>> print(sys.argv)
['/usr/local/lib/python3.5/dist-packages/spyder/utils/ipython/start_kernel.py', '-f', '/run/user/1000/jupyter/kernel-690215617a08.json']

What steps will reproduce the problem?

  1. Run code snippet above in ipython console
  2. An error message is generated

The code snippet does work when executed in the python console. It also worked with previous versions of spyder. The issue is probably the same as #1557.

Versions and main components

  • Spyder Version: Spyder 4.0.0.dev0
  • Python Version: 3.5.2
  • Operating system: Ubuntu
IPython Console Completed Enhancement

Most helpful comment

@andfoy, please work on this one. The idea is to clean sys.argv in utils/ipython/start_kernel.py by evaluating

import sys; sys.argv=['']; del sys

in the same way as we evaluate the matplotlib magic.

All 8 comments

What does -f have to do with this?

The -f is not one of the arguments in my parser object, that is why the line args = parser.parse_args() fails. To compare: if I run the code snippet from the command line, then the follow works:

python snippet.py

but the following fails:

python -f dummyvalue snippet.py

So the solution would be to clear sys.argv for our IPython consoles, right?

That would solve it for me yes. Not sure how easy that is to do in spyder. Note that if I run the code with F5 then the arguments are cleared.

@andfoy, please work on this one. The idea is to clean sys.argv in utils/ipython/start_kernel.py by evaluating

import sys; sys.argv=['']; del sys

in the same way as we evaluate the matplotlib magic.

@ccordoba12 your answer solve my problem , thank you!

You can also try args = parser.parse_args(args=[])

i have the same error how you fix it?
Exception has occurred: SystemExit
2
File "C:UsersAcerDownloadsimgclsmob-masterimgclsmob-masterexamplesdemo_tf2.py", line 64, in parse_args
args = parser.parse_args()
File "C:UsersAcerDownloadsimgclsmob-masterimgclsmob-masterexamplesdemo_tf2.py", line 72, in main
args = parse_args()
File "C:UsersAcerDownloadsimgclsmob-masterimgclsmob-masterexamplesdemo_tf2.py", line 132, in
main()

Was this page helpful?
0 / 5 - 0 ratings