Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error when executing argparse in IPython console #3883

Closed
peendebak opened this issue Dec 23, 2016 · 8 comments
Closed

Error when executing argparse in IPython console #3883

peendebak opened this issue Dec 23, 2016 · 8 comments

Comments

@peendebak
Copy link

peendebak commented Dec 23, 2016

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
@ccordoba12
Copy link
Member

What does -f have to do with this?

@peendebak
Copy link
Author

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

@ccordoba12
Copy link
Member

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

@peendebak
Copy link
Author

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.

@ccordoba12 ccordoba12 added this to the v3.1 milestone Dec 25, 2016
@ccordoba12
Copy link
Member

@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 ccordoba12 changed the title error when executing argparse in ipython console Error when executing argparse in IPython console Jan 5, 2017
@lvjiujin
Copy link

@ccordoba12 your answer solve my problem , thank you!

@ArvindSubramaniam
Copy link

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

@fatimajanbein
Copy link

i have the same error how you fix it?
Exception has occurred: SystemExit
2
File "C:\Users\Acer\Downloads\imgclsmob-master\imgclsmob-master\examples\demo_tf2.py", line 64, in parse_args
args = parser.parse_args()
File "C:\Users\Acer\Downloads\imgclsmob-master\imgclsmob-master\examples\demo_tf2.py", line 72, in main
args = parse_args()
File "C:\Users\Acer\Downloads\imgclsmob-master\imgclsmob-master\examples\demo_tf2.py", line 132, in
main()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants