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?
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.
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()
Most helpful comment
@andfoy, please work on this one. The idea is to clean
sys.argv
inutils/ipython/start_kernel.py
by evaluatingin the same way as we evaluate the matplotlib magic.