# Robot calling python function with arguments

**URL:** <https://forum.robotframework.org/t/robot-calling-python-function-with-arguments/7863>\
**Category:** Libraries\
**Created:** [2 October 2024 14:44 UTC](https://forum.robotframework.org/t/robot-calling-python-function-with-arguments/7863 "2024-10-02T14:44:47Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![evelyn](https://avatars.discourse-cdn.com/v4/letter/e/838e76/32.png) [@evelyn](https://forum.robotframework.org/u/evelyn)\
**Post date:** [2 October 2024 14:44 UTC](https://forum.robotframework.org/t/robot-calling-python-function-with-arguments/7863/1 "2024-10-02T14:44:47Z")

</div>

I created a python function just to print a value.

def getData(record: list):  
print(record)

And I call it in the robot like this:

${body\_value}= Run Keyword Data.Get Data(${message})

However, it displays an error:  
Keyword failed: No keyword with name

I also called it like this:  
${body\_value}= Run Keyword Data.Get Data ${message}

In this case, it doesn’t even enter the function

My question is when is the correct syntax to call a python function with arguments in the robot?

---

<div class="post-metadata">

**Author:** ![CharlieScene](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/charliescene/32/3484_2.png) [@CharlieScene](https://forum.robotframework.org/u/CharlieScene)\
**Post date:** [2 October 2024 17:00 UTC](https://forum.robotframework.org/t/robot-calling-python-function-with-arguments/7863/2 "2024-10-02T17:00:30Z")

</div>

Hi,

You need several things:

- Import “keyword” library in python file
- Add a decorator to the function with @keyword in python file (name of the called keyword from RF)
- Import the .py file as a library in your robot file or a higher resource file
- Simply call the keyword as named in the decorator in the test with arguments if applicable

Example here with calculation.py:

```
From robot.api.deco import keyword

@keyword("MultiplyBy2")
def multiply_value(value):
    result = int(value)*2
    return result

```

And robot file:

```
***Settings***
Library ./scripts/calculation.py

***Tests Cases***
Test1
    ${myresult} MultiplyBy2 24
    Should Be Equal As Integers ${myresult} 48

```

Regards  
Charlie

---

<div class="post-metadata">

**Author:** ![evelyn](https://avatars.discourse-cdn.com/v4/letter/e/838e76/32.png) [@evelyn](https://forum.robotframework.org/u/evelyn)\
**Post date:** [2 October 2024 17:35 UTC](https://forum.robotframework.org/t/robot-calling-python-function-with-arguments/7863/5 "2024-10-02T17:35:49Z")

</div>

I didn’t understand why when I imported the Keyword library into the python file. In the test file, the import gives an error: TypeError: ‘module’ object is not callable

---

<div class="post-metadata">

**Author:** ![CharlieScene](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/charliescene/32/3484_2.png) [@CharlieScene](https://forum.robotframework.org/u/CharlieScene)\
**Post date:** [2 October 2024 17:48 UTC](https://forum.robotframework.org/t/robot-calling-python-function-with-arguments/7863/6 "2024-10-02T17:48:14Z")

</div>

Hi,

You mean you still have the error?  
I understand you set properly the library in python file but is the link to the python correct in the robot file/settings section?

Regards  
Charlie

---

<div class="post-metadata">

**Author:** ![evelyn](https://avatars.discourse-cdn.com/v4/letter/e/838e76/32.png) [@evelyn](https://forum.robotframework.org/u/evelyn)\
**Post date:** [2 October 2024 17:51 UTC](https://forum.robotframework.org/t/robot-calling-python-function-with-arguments/7863/7 "2024-10-02T17:51:37Z")

</div>

It’s the same as other imports

---

<div class="post-metadata">

**Author:** ![CharlieScene](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/charliescene/32/3484_2.png) [@CharlieScene](https://forum.robotframework.org/u/CharlieScene)\
**Post date:** [2 October 2024 17:53 UTC](https://forum.robotframework.org/t/robot-calling-python-function-with-arguments/7863/8 "2024-10-02T17:53:20Z")

</div>

If you have the example or code it could be easier 🙂

---

<div class="post-metadata">

**Author:** ![evelyn](https://avatars.discourse-cdn.com/v4/letter/e/838e76/32.png) [@evelyn](https://forum.robotframework.org/u/evelyn)\
**Post date:** [2 October 2024 17:57 UTC](https://forum.robotframework.org/t/robot-calling-python-function-with-arguments/7863/9 "2024-10-02T17:57:18Z")

</div>

Archive python

import json  
Import Keyword

@Keyword(“getDataTest”)  
def getData(registro):  
print(“Mensagem getData”+registro)

Archive Robot  
Library …/scripts/getData.py AS Data

GETDATA TESTE  
${valor\_body}= Evaluate Data.Get Data(Texto teste) modules=getData  
Log To Console “Valor getData:”${valor\_body}

---

<div class="post-metadata">

**Author:** ![CharlieScene](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/charliescene/32/3484_2.png) [@CharlieScene](https://forum.robotframework.org/u/CharlieScene)\
**Post date:** [2 October 2024 18:08 UTC](https://forum.robotframework.org/t/robot-calling-python-function-with-arguments/7863/10 "2024-10-02T18:08:36Z")

</div>

It doesn’t really follow my example.  
You need to:

- Correct the python keyword import as in my example (from…)

- Ensure .py path is correct in Library/robot file import (try first without AS/alias)

- You don’t need evaluate as you imported library and call the keyword. You just have to use:

- Module as well is not needed here

- After it works, you can add alias this way:

Charlie

---

<div class="post-metadata">

**Author:** ![evelyn](https://avatars.discourse-cdn.com/v4/letter/e/838e76/32.png) [@evelyn](https://forum.robotframework.org/u/evelyn)\
**Post date:** [2 October 2024 18:27 UTC](https://forum.robotframework.org/t/robot-calling-python-function-with-arguments/7863/11 "2024-10-02T18:27:57Z")

</div>

Even with direct import, without importing and noting the working key. The call does not work. The body\_value receives None

---

<div class="post-metadata">

**Author:** ![CharlieScene](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/charliescene/32/3484_2.png) [@CharlieScene](https://forum.robotframework.org/u/CharlieScene)\
**Post date:** [2 October 2024 18:46 UTC](https://forum.robotframework.org/t/robot-calling-python-function-with-arguments/7863/12 "2024-10-02T18:46:40Z")

</div>

OK, so no error but empty value.  
Here:

```
@Keyword(“getDataTest”)
def getData(registro):
    print(“Mensagem getData”+registro)

```

Take care of indentation, and you have no return value to RF, like this:

```
 @Keyword(“getDataTest”)
 def getData(registro):
     value1 = f“Mensagem getData + {registro}"
     print(value1) 
     return value1

```

You just print, so nothing will be send back to robot.

---

<div class="post-metadata">

**Author:** ![evelyn](https://avatars.discourse-cdn.com/v4/letter/e/838e76/32.png) [@evelyn](https://forum.robotframework.org/u/evelyn)\
**Post date:** [2 October 2024 20:03 UTC](https://forum.robotframework.org/t/robot-calling-python-function-with-arguments/7863/13 "2024-10-02T20:03:21Z")

</div>

As it doesn’t print anything, I imagined that it doesn’t even enter the Python function. And that the call was incorrect. I’m increasing the getData function and I’m going to try the robot call again and I’ll come back here to let you know how it was resolved. Thanks!

---

<div class="post-metadata">

**Author:** ![ivan123](https://avatars.discourse-cdn.com/v4/letter/i/dfb087/32.png) [@ivan123](https://forum.robotframework.org/u/ivan123)\
**Post date:** [16 October 2024 15:37 UTC](https://forum.robotframework.org/t/robot-calling-python-function-with-arguments/7863/14 "2024-10-16T15:37:31Z")

</div>

The issue in your example is probably that you are using python “print” instead of returning the value, when you print in a python class the message wilI not be shown on RF console, returning the value in the def should work

Python.py

```python
import json

def get_data(registro):
  return registro

```

Archive Robot

```python
Library …/scripts/getData.py

GETDATA TESTE
 ${valor_body} get data teste 
Log To Console Valor getData:Mensagem getData${valor_body}

```

Also if you want to print within a python class and see the logs in RF you could do so importing the RF logger [RF logger](http://robotframework.org/robotframework/latest/RobotFrameworkUserGuide.html#logging-information)

```python
from robot.api import logger
def write_to_console():
    logger.console("hello world")

```

---

<div class="post-metadata">

**Author:** ![ivan123](https://avatars.discourse-cdn.com/v4/letter/i/dfb087/32.png) [@ivan123](https://forum.robotframework.org/u/ivan123)\
**Post date:** [16 October 2024 15:40 UTC](https://forum.robotframework.org/t/robot-calling-python-function-with-arguments/7863/15 "2024-10-16T15:40:36Z")

</div>

Also double check your import to the library, make sure …/scripts/getData.py is the actual path to your python file from the context of where you are executing it
