2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

【Google colab】argparseでエラーが出た場合の対処法

Last updated at Posted at 2022-01-30

環境

Google Colaboratory

現象

以下のコードを Google colab で実行すると、次のようなエラーが出る

Python
import argparse

parser = argparse.ArgumentParser(description="argparse test")
parser.add_argument('-me', '-max_epoch', type=int, default=200)
parser.add_argument('-ms', '-minibatch_size', type=int, default=256)
parser.add_argument('-g', '-gpu', type=int, default=-1)
parser.add_argument('-path', default='./')
args = parser.parse_args()

print(args)
出力
usage: ipykernel_launcher.py [-h] [-me ME] [-ms MS] [-g G] [-path PATH]
ipykernel_launcher.py: error: unrecognized arguments: -f /root/.local/share/jupyter/runtime/kernel-d19b24d6-a0cd-4fbc-9299-7feb199b5ddc.json
An exception has occurred, use %tb to see the full traceback.

SystemExit: 2

対処法

上のコードで args = parser.parse_args()args = parser.parse_args(args=[]) に変えるとエラーが消える

出力
Namespace(g=-1, me=200, ms=256, path='./')

これで args.xx が使えるようになる

参考

2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?