NameError: name 'argv' is not defined
Asked Answered
E

1

7

I am trying to make a Google API call and am getting an error with the beginning of the code found here:

import os

import argparse
import sys

from apiclient import sample_tools
from oauth2client import client

# Declare command-line flags.
argparser = argparse.ArgumentParser(add_help=False)
argparser.add_argument(
    'profile_id', type=int,
    help='The ID of the profile to look up campaigns for')



  # Authenticate and construct service.
service, flags = sample_tools.init(
    argv[1:], 'dfareporting', 'v2.1', __doc__, os.path.abspath(os.path.dirname("__file__")), parents=[argparser],
    scope=['https://www.googleapis.com/auth/dfareporting',
           'https://www.googleapis.com/auth/dfatrafficking'])

if __name__ == '__main__':
  main(sys.argv)

However, the sample_tools.init function is not returning the service and flags object. I think I have isolated it to the argv argument a

NameError: name 'argv' is not defined

Any help would be much appreciated!!!

Egocentrism answered 7/8, 2015 at 21:36 Comment(1)
I think you need sys.argv?Rubellite
S
11

You are missing sys:

sys.argv[1:]

You either need to from sys import argv and use argv everywhere or import sys as you have and use sys.argv. I also don't see a main function anywhere so main(sys.argv) is also going to cause a NameError

Socage answered 7/8, 2015 at 21:37 Comment(2)
Oh yes! I left it out in the paste but def main(argv): came before service,flags = . I tried all of those suggestions and I am still not able to get that function to work - but it does appear that argv is now defined as [''] - if that is correct. Any other ideas would be appreciated!!!Egocentrism
How is it not working? Are you passing args to the script?Socage

© 2022 - 2024 — McMap. All rights reserved.