7

May I know what is the best practice to debug an argpars function.

Say I have a py file test_file.py with the following lines

# Script start
import argparse
import os
parser = argparse.ArgumentParser()
parser.add_argument(“–output_dir”, type=str, default=”/data/xx”)
args = parser.parse_args()
os.makedirs(args.output_dir)
# Script stop

The above script can be executed from terminal by:

python test_file.py –output_dir data/xx

However, for debugging process, I would like to avoid using terminal. Thus the workaround would be

# other line were commented for debugging process
# Thus, active line are
# Script start
import os
args = {“output_dir”:”data/xx”}
os.makedirs(args.output_dir)
#Script stop

However, I am unable to execute the modified script. May I know what have I miss?

3
  • 1
    What did you write this in? You've got curly quotes and dashes in your code. If you're using a word processor instead of a text editor, that's a bad idea for programming. Commented Jun 15, 2018 at 19:10
  • 1
    args is a Namespace object, not a dict. However, for debugging you can simply pass an explicit list of arguments to parse_args, for example args = parser.parse_args(["output_dir", "data/xx"]). Commented Jun 15, 2018 at 19:13
  • HI @chepner thanks for the input. But may I know how to implement this for a multiple inputs with different class problem. I had posted the problem in a new thread. Thanks in advance Commented Jun 16, 2018 at 9:08

2 Answers 2

3

When used as a script, parse_args will produce a Namespace object, which displays as:

argparse.Namespace(output_dir='data/xx')

then

args.output_dir

will be the value of that attribute

In the test you could do one several things:

args = parser.parse_args([....])  # a 'fake' sys.argv[1:] list

args = argparse.Namespace(output_dir= 'mydata')

and use args as before. Or simply call the

os.makedirs('data/xx')

I would recommend organizing the script as:

# Script start
import argparse
import os
# this parser definition could be in a function
parser = argparse.ArgumentParser()
parser.add_argument(“–output_dir”, type=str, default=”/data/xx”)

def main(args):
    os.makedirs(args.output_dir)

if __name__=='__main__':
    args = parser.parse_args()
    main(args)

That way the parse_args step isn't run when the file is imported. Whether you pass the args Namespace to main or pass values like args.output_dir, or a dictionary, etc. is your choice.

Sign up to request clarification or add additional context in comments.

1 Comment

HI @hpaulj, thanks for the input. While your suggestion work for single input, I am having difficulties in implementing it for multiple input with different class problem. I post the problem in a new thread, which can be found in the link link
1

You can write it in a shell script to do what you want

bash:

#!/usr/bin/

cd /path/to/my/script.py
python script.py --output_dir data/xx 

If that is insufficient, you can store your args in a json config file

configs.json

{"output_dir": "data/xx"}

To grab them:

import json
with open('configs.json', 'rb') as fh:
    args = json.loads(fh.read())

output_dir = args.get('output_dir')

# 'data/xx'

Do take note of the double quotes around your keys and values in the json file

1 Comment

Thanks for the inputs. appreciate it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.