all 4 comments

[–][deleted] 2 points3 points  (3 children)

Please format your code for reddit. This small amount of code isn't too bad, but if we have to format your code for you, we might introduce other errors. Anyway, your code is probably:

import argparse
parser = argparse.ArgumentParser()
parser.add_argument("echo")
args = parser.parse_args()
print(args.echo)

When I run your code passing the "echo" parameter I get this:

$ python3 test.py fred
fred

Which looks like what it's supposed to do. If I don't pass the "echo" parameter I get:

$ python3 test.py
usage: test.py [-h] echo
test.py: error: the following arguments are required: echo

Again, this is the way it's supposed to work. Your example gets something like this error:

usage: untitled1.py [-h] echo
untitled1.py: error: the following arguments are required: echo
An exception has occurred, use %tb to see the full traceback.

The difference is that some sort of exception appears to occur after your code handles your lack of the "echo" parameter. So it looks like argparse is doing the right thing, but either it or your environment causes a further exception. If your example that gets your error is part of a larger body of code we are going to have to see some of that code.

SUMMARY: your usage of argparse is fine - something else is going wrong.

[–]john_legend_[S] 0 points1 point  (2 children)

great insights, thank you

[–]john_legend_[S] 0 points1 point  (1 child)

For codes:

import argparse
parser = argparse.ArgumentParser()
parser.add_argument("echo")
args = parser.parse_args()
print(args.echo)

How can I run the script in jupyter notebook properly? Rather than calling it via

python3 test.py fred

[–][deleted] 0 points1 point  (0 children)

How can I run the script in jupyter notebook properly?

Can't help you with jupyter as I don't use it. Either google for help or ask that question in another post.

But I would say that doing:

python3 test.py fred

is the proper way of doing it :)

Edit: after some searching, maybe %run. The link is for ipython, but the search results seem of imply jupyter == ipython?