Chenguang Xiao

Logo

Chenguang is currently a PhD candidate in computer science.

View My GitHub Profile

29 May 2023

Use python argparse in Jupyter Notebook

by Chenguang Xiao

Python built-in module argparse is a very useful tool to parse command line arguments. In prodcution, we usually run python scripts in command line. When import a package contain argparse in Jupyter Notebook, it will raise an error.

For example we get a python script test.py with argparse:

import argparse

def get_args():
    parser = argparse.ArgumentParser()
    parser.add_argument('--test', type=int, default=0)
    args = parser.parse_args()
    return args

if __name__ == '__main__':
    args = get_args()
    print(args.test)

there is no problem to run it in command line:

python test.py --test 1

However, if we import it in Jupyter Notebook, it will raise an error:

from test import get_args
args = get_args()

You will get a empty Namespace object for args variable rather than args.test=0.

This can be useful when we want to test or debug our code in Jupyter Notebook. Here is a simple solution to solve this problem.

# add following code before calling argparse
import sys
sys.argv = ['']

# run your code
from test import get_args
args = get_args()

Now you can use argparse in Jupyter Notebook normally as in comdline line to get the default value of your configuration.

tags: