cancel
Showing results for 
Search instead for 
Did you mean: 
Data Engineering
Join discussions on data engineering best practices, architectures, and optimization strategies within the Databricks Community. Exchange insights and solutions with fellow data engineers.
cancel
Showing results for 
Search instead for 
Did you mean: 

How do I pass kwargs to wheel method?

ACK
New Contributor II

Hi,

I have a method named main it takes **kwargs as a parameter.

def main(**kwargs):
    parameterOne = kwargs["param-one"]
    parameterTwo = kwargs["param-two"]
    parameterThree = kwargs["param-optional-one"] if "param-optional-one" in kwargs else None
    parameterFour = kwargs["param-optional-two"] if "param-optional-two" in kwargs else None

I have declared it as an entry_point in setup

entry_points={
        'console_scripts': [
            'main=ETL.V1:main'
        ]
    },

I am passing keyword arguments in task

"entry_point": "main",
                    "named_parameters": {
                        "param-one": "test",
                        "param-two": "test two"
                    }

when I run the job it goes into main method, but fails on the very first line

KeyError: 'param-one'
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<command--1> in <module>
     16 if entry:
     17   # Load and execute the entrypoint, assumes no parameters
---> 18   entry[0].load()()
     19 else:
     20   import ETL.V1
 
/databricks/python/lib/python3.8/site-packages/ETL/V1.py in main(**kwargs)
      8     for i, v in kwargs.items():
      9         print("    ", i, ": ", v)
---> 10     parameterOne = kwargs['param-one']
     11     parameterTwo = kwargs['param-two']
 
KeyError: 'param-one'

The keyword arguments are not being passed or I am doing something wrong?

I tried to see if anything is in kwargs, however that didn't print anything in the task output just the error above was displayed. So, I can't really tell if anything is in kwargs or not.

1 ACCEPTED SOLUTION

Accepted Solutions

Hubert-Dudek
Esteemed Contributor III

it is command-line parameters so it is like

---param-one=test

you can test it with ArgumentParser

from argparse import ArgumentParser
 
parser = ArgumentParser()
parser.add_argument("--param-one", dest="parameterOne")
 
args = parser.parse_args()

View solution in original post

2 REPLIES 2

Hubert-Dudek
Esteemed Contributor III

it is command-line parameters so it is like

---param-one=test

you can test it with ArgumentParser

from argparse import ArgumentParser
 
parser = ArgumentParser()
parser.add_argument("--param-one", dest="parameterOne")
 
args = parser.parse_args()

ACK
New Contributor II

Hi,

Thanks for you answer.

Yes, what you say is correct, we must use argparse to get the keyword arguments.

Though, doing argparse outside of the entry point method didn't work for me.

I had to do it inside the method.

I have no idea why? I will try it again in an hour, for now I was happy to be able to get it working.

Thanks

I will select your answer.

I will comment back here if it can be down outside of the entry method.

Join 100K+ Data Experts: Register Now & Grow with Us!

Excited to expand your horizons with us? Click here to Register and begin your journey to success!

Already a member? Login and join your local regional user group! If there isn’t one near you, fill out this form and we’ll create one for you to join!