0

I have a function:

def UPLOAD(FILE_NAME):


    try:

        client = bigquery.Client()

        dataset_ref = client.dataset(DATASET)
        table_ref = dataset_ref.table(TABLE)
        job_config = bigquery.LoadJobConfig()
        job_config.source_format = bigquery.SourceFormat.NEWLINE_DELIMITED_JSON
        job_config.autodetect = False

        with open(FILE_NAME, 'rb') as source_file:
            job = client.load_table_from_file(
                source_file,
                table_ref,
                location='EU',  # Must match the destination dataset location.
                job_config=job_config)  # API request

        job.result()  # Waits for table load to complete.

    except:
        error(job,'BLABLA')

The issue in this function that when I try to pass the job parameter from except to error function it not picking it up.

What is the actual error, I'm not sure...

ERROR: Error: local variable 'job' referenced before assignment

UPDATE:

def error(job,extra):
    if extra == 'BLABLA':
        ERROR = job.error
    elif extra == 'LALA':
        ERROR = job.error
    else:
        print('else')
Rex5
  • 771
  • 9
  • 23
Oksana Ok
  • 515
  • 3
  • 7
  • 19
  • Well, where does `extra` come from? Where is it declared? – m02ph3u5 Jul 29 '19 at 08:37
  • @m02ph3u5 have added error function – Oksana Ok Jul 29 '19 at 08:40
  • Possible duplicate of [How do I check if a variable exists?](https://stackoverflow.com/questions/843277/how-do-i-check-if-a-variable-exists) – Mp0int Jul 29 '19 at 08:49
  • 3
    There is a possibility that exception occured far before you declare job with `job = client.load_table_from_file` .It is more secure to check whether that variable is defined at the time of exception since python scope is a bit different than C/Java/C# or relevant other language scopes. – Mp0int Jul 29 '19 at 08:51
  • @FallenAngel thank you for your suggestion, I have fixed the issue by lower down `try:`. Thank you again ;) – Oksana Ok Jul 29 '19 at 09:16

1 Answers1

0

have you tried defining job at the start of your try statement? it seems that the exception is happening well before the variable has even been defined, so the try statement is exiting before job even exists!!

3NiGMa
  • 545
  • 1
  • 9
  • 24