2

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

6
  • 1
    How are you calling run? The error description is pretty clear: run accepts 0 arguments but is being passed one by whatever is calling it.
    – SyntaxVoid
    Dec 12, 2019 at 23:51
  • I've past the name of my function in the function to execute field in Google Cloud Function. I'm running my function using the testing trigger. Dec 12, 2019 at 23:53
  • I'm not familiar at all with google.cloud, but I'm guessing google is passing some kind of event object to your run function. Someone with more knowledge can build off of this, but in order to figure out exactly what the hack is going on, I would modify run to accept a single argument and print the value of that argument right away to see what it looks like.
    – SyntaxVoid
    Dec 12, 2019 at 23:58
  • I suggest stripping this down to the minimum code that recreates the issue. If you remove all traces of bigquery, I think you will still have the same problem. So, please do the following: edit the question to show the minimal code, and also show how you are deploying this function with gcloud. stackoverflow.com/help/mcve Dec 13, 2019 at 0:11
  • but if I'm removing traces of BigQuery, there is nothing left no? The only thing which is not bigquery related is the csv_file variable. and like I said the function is deployed through Google Cloud Functions. Dec 13, 2019 at 0:14

1 Answer 1

8

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.

5
  • I have past my entire code in the question so no run(arg) I guess. Dec 13, 2019 at 0:01
  • Since that's all of your code, it's apparent that Google Cloud is calling your function and expects it to take an argument. I've updated my answer.
    – rnorris
    Dec 13, 2019 at 0:15
  • Adding up the argument crashes when running locally. How to execute locally?
    – Augusto
    Jul 5, 2022 at 1:07
  • @Augusto, can you create a new question with details of what you're trying to do? I'm not sure how you're setting up the lambda/cloud function to execute locally, but it's likely that you'll need to mock the request object that is normally passed in by the AWS/GCP environment.
    – rnorris
    Jul 8, 2022 at 11:14
  • @rnorris I have created one, could please check: stackoverflow.com/questions/72863004/…
    – Augusto
    Jul 8, 2022 at 13:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.