2

I have implemented argparse into a Python script like so:

parser = argparse.ArgumentParser()
parser.add_argument("-s", "--shortterm", help="Tweets top information from the past month", default="track",
                    choices=choices, dest="shortterm")
parser.add_argument("-m", "--mediumterm", help="Tweets top information from the past 6 months", default="track",
                    choices=choices)
parser.add_argument("-l", "--longterm", help="Tweets top information from the past few years", default="track",
                    choices=choices)
args = parser.parse_args()

Which I then check args for things the user could have entered or selected, like so:

if args.mediumterm:
    if args.mediumterm == "all":
        get_top_tracks(medium_term)
        get_top_artists(medium_term)
    else:
        if args == "track":
            get_top_tracks(medium_term)
        elif args == "artist":
            get_top_artists(medium_term)

When I run the script with the following command:

python top_tracks_artists_spotify_time.py --mediumterm all

I get the following error:

Traceback (most recent call last):
File "top_tracks_artists_spotify_time.py", line 127, in <module>
if args.mediumterm:
AttributeError: 'str' object has no attribute 'mediumterm'

The annoying thing is running:

python top_tracks_artists_spotify_time.py --shortterm all

runs the script successfully.

EDIT: I have added dest="mediumterm" to the argparse to no avail

7
  • where/how are you defining args? Commented Aug 1, 2018 at 23:12
  • print to see what you are looking for: print(args) print(args.__dict__) print(dir(args)) Commented Aug 1, 2018 at 23:12
  • @aydow added the code. it has always been there, just not copied and pasted into the question by mistake Commented Aug 1, 2018 at 23:13
  • 1
    args is a string in this context: args.mediumterm so I don't understand how that is possible. At some point you are transforming args from a dict/object into a string. Commented Aug 1, 2018 at 23:19
  • 2
    Search for args = before the line 127 and make sure you're not overwriting it. Commented Aug 1, 2018 at 23:20

1 Answer 1

1

Your processing code, after args = parser.parse_args() should look something like:

term = args.mediumterm
if term:
    if term == "all":
        get_top_tracks(term)     # unless medium_term is defined else where
        get_top_artists(term)
    else:
        if term == "track":
            get_top_tracks(term)
        elif term == "artist":
            get_top_artists(term)

Similarly for shortterm and longterm. Once created by parse_args, args should not be reassigned (it will only confuse you and your readers).

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

1 Comment

This makes sense and is systematically a better way to do it it seems. Thanks

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.