Simon Breton
Simon Breton

Reputation: 2876

"takes 0 positional arguments but 1 was given" error

I'm running the python script below on Google Cloud Functions. When testing the script I have the following error:

Error: function terminated. Recommended action: inspect logs for termination reason. Details: run() takes 0 positional arguments but 1 was given

What does it mean?

Here is my script:

from google.cloud import bigquery

client = bigquery.Client()

def run():
    csv_file = six.BytesIO(b"""full_name,age
Phred Phlyntstone,32
Wylma Phlyntstone,29
""")

    table_ref = dataset.table('ga-online-audit:test_python.test')
    job_config = bigquery.LoadJobConfig()
    job_config.source_format = 'CSV'
    job_config.skip_leading_rows = 1
    job = client.load_table_from_file(
        csv_file, table_ref, job_config=job_config)  # API request
    job.result()  # Waits for table load to complete.

As I'm learning I took this script from the following documentation https://google-cloud-python.readthedocs.io/en/0.32.0/bigquery/usage.html

Upvotes: 6

Views: 4981

Answers (1)

rnorris
rnorris

Reputation: 2082

Since you have posted all of your code, it's apparent that something else is calling your function. Looking at a Google Cloud Function example in python, it looks like your function must be defined so that it takes at least one argument.

The code in the linked article has def hello_world(request), in this case, request is the argument that is passed when you call the cloud function. AWS Lambdas are similar, in that they pack any URL parameters or JSON payload from the client into this request argument, so that's likely what's going on here.

I would recommend adding an argument to the definition of run. This will fix your error, and inspecting the argument will give you insight on what kind of information the Google cloud function platform is automatically sending to your code.

Upvotes: 9

Related Questions