Issue
Say I have this file:
import argparse
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--foo', action='store_true')
args = parser.parse_args()
print(args)
If I run it with Python, it works fine:
% python -m t --foo
Namespace(foo=True)
However, running it with IPython, I get an error:
% ipython -m t --foo
usage: ipython [-h] [--debug] [--quiet] [--init] [--autoindent] [--no-autoindent] [--automagic] [--no-automagic] [--pdb] [--no-pdb] [--pprint]
[--no-pprint] [--color-info] [--no-color-info] [--ignore-cwd] [--no-ignore-cwd] [--nosep] [--autoedit-syntax]
[--no-autoedit-syntax] [--simple-prompt] [--no-simple-prompt] [--banner] [--no-banner] [--confirm-exit] [--no-confirm-exit]
[--term-title] [--no-term-title] [--classic] [--quick] [-i] [--profile-dir ProfileDir.location]
[--profile TerminalIPythonApp.profile] [--ipython-dir TerminalIPythonApp.ipython_dir] [--log-level TerminalIPythonApp.log_level]
[--config TerminalIPythonApp.extra_config_file] [--autocall TerminalInteractiveShell.autocall]
[--colors TerminalInteractiveShell.colors] [--logfile TerminalInteractiveShell.logfile]
[--logappend TerminalInteractiveShell.logappend] [-c TerminalIPythonApp.code_to_run] [-m TerminalIPythonApp.module_to_run]
[--ext TerminalIPythonApp.extra_extensions] [--gui TerminalIPythonApp.gui] [--pylab [TerminalIPythonApp.pylab]]
[--matplotlib [TerminalIPythonApp.matplotlib]] [--cache-size TerminalInteractiveShell.cache_size]
[extra_args [extra_args ...]]
ipython: error: argument --foo: expected one argument
How can I run it using IPython
?
(my end-goal is to run be able to access a running IPython kernel from within my script)
Solution
As @hpaulj said, you can use --
as a separator to indicate to IPython to stop processing arguments. This is a commonly used separator in unix commands.
Answered By - Matt
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.