Argparse in iPython notebook: unrecognized arguments: -f

18,509

Solution 1

It's better to use @nbro 's answer for Jupyter execution.

args = parser.parse_args(args=[])

If you want to manage parameters as class format, you can try this.

class Args:
  data = './data/penn'
  model = 'LSTM'
  emsize = 200
  nhid = 200

args=Args()

Solution 2

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

Solution 3

As @nbro suggested, the following command should work:

args = parser.parse_args(args=[])

In addition, if you have required arguments in your parser, set them inside the list:

args = parser.parse_args(args=['--req_1', '10', '--req_2', '10'])

Where you previously used:

import argparse
parser = argparse.ArgumentParser(description="Dummy parser")
parser.add_argument("--req_1", type=int, required=True, help="required int 1")
parser.add_argument("--req_2", type=int, required=True, help="required int 2")

You can also see from the notebook all params:

print("see all args:", args)
print("use one arg:", args.req_1)

You can find more information in the docs: Parsing arguments

Solution 4

An example is:

import argparse
parser = argparse.ArgumentParser()
parser.add_argument('echo')
args = parser.parse_args(['aa']) # actually you don't have to write (args=['aa'])
print(args.echo)

the output should be

>>> aa
Share:
18,509

Related videos on Youtube

Paul
Author by

Paul

Updated on September 15, 2022

Comments

  • Paul
    Paul over 1 year

    I am trying to pass a .py file to ipython notebook environment. I have never had to deal directly with argparse before. How do I rewrite the main() function?

    I tried to delete the line of def main(): and keep the rest of the code.

    But args = parser.parse_args()" returned an error:

    ipykernel_launcher.py: error: unrecognized arguments: -f.

    And when I run . %tb: showing this

    def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('--data_dir', type=str, default='data/tinyshakespeare',
                       help='data directory containing input.txt')
    parser.add_argument('--input_encoding', type=str, default=None,
                       help='character encoding of input.txt, from https://docs.python.org/3/library/codecs.html#standard-encodings')
    parser.add_argument('--log_dir', type=str, default='logs',
                       help='directory containing tensorboard logs')
    parser.add_argument('--save_dir', type=str, default='save',
                       help='directory to store checkpointed models')
    parser.add_argument('--rnn_size', type=int, default=256,
                       help='size of RNN hidden state')
    parser.add_argument('--num_layers', type=int, default=2,
                       help='number of layers in the RNN')
    parser.add_argument('--model', type=str, default='lstm',
                       help='rnn, gru, or lstm')
    parser.add_argument('--batch_size', type=int, default=50,
                       help='minibatch size')
    parser.add_argument('--seq_length', type=int, default=25,
                       help='RNN sequence length')
    parser.add_argument('--num_epochs', type=int, default=50,
                       help='number of epochs')
    parser.add_argument('--save_every', type=int, default=1000,
                       help='save frequency')
    parser.add_argument('--grad_clip', type=float, default=5.,
                       help='clip gradients at this value')
    parser.add_argument('--learning_rate', type=float, default=0.002,
                       help='learning rate')
    parser.add_argument('--decay_rate', type=float, default=0.97,
                       help='decay rate for rmsprop')
    parser.add_argument('--gpu_mem', type=float, default=0.666,
                       help='%% of gpu memory to be allocated to this process. Default is 66.6%%')
    parser.add_argument('--init_from', type=str, default=None,
                       help="""continue training from saved model at this path. Path must contain files saved by previous training process:
                            'config.pkl'        : configuration;
                            'words_vocab.pkl'   : vocabulary definitions;
                            'checkpoint'        : paths to model file(s) (created by tf).
                                                  Note: this file contains absolute paths, be careful when moving files around;
                            'model.ckpt-*'      : file(s) with model definition (created by tf)
                        """)
    args = parser.parse_args()
    train(args)
    
  • Salih Karagoz
    Salih Karagoz over 5 years
    You saved my day dude.
  • partizanos
    partizanos about 3 years
    Solution of @nbro is easier and doesn't require modifying existing parser
  • Alaa M.
    Alaa M. almost 3 years
    Can I pass the required argument values from the other notebook?