# Running Robot Framework keywords from Python Function

**URL:** <https://forum.robotframework.org/t/running-robot-framework-keywords-from-python-function/5428>\
**Category:** Robot Framework\
**Created:** [10 March 2023 17:09 UTC](https://forum.robotframework.org/t/running-robot-framework-keywords-from-python-function/5428 "2023-03-10T17:09:03Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![SKottapalli](https://avatars.discourse-cdn.com/v4/letter/s/9f8e36/32.png) [@SKottapalli](https://forum.robotframework.org/u/SKottapalli)\
**Post date:** [10 March 2023 17:09 UTC](https://forum.robotframework.org/t/running-robot-framework-keywords-from-python-function/5428/1 "2023-03-10T17:09:03Z")

</div>

Hi - I was trying to run a Robot Framework keyword from a Python function and capture the JSON/data returned from the keyword and do some additional logic and validation that was already implemented in Python. Calling Python function from a Robot test suite is no issue, but can we do other way around? I found below thread and tried following the same. but When I call this Python function externally as a Flask application I see this error. Can Python function access any robot resource files /keywords from the project ?

> <https://stackoverflow.com/questions/27043670/execute-robot-keyword-from-python-using-robotframework-api>

sample code tried:

libraries.BuiltIn.RobotNotRunningError: Cannot access execution context

from robot.libraries.BuiltIn import BuiltIn  
from flask import Flask, request

app = Flask( **name** )

@app.get(‘/myEndPoint’)  
def call\_keyword():  
myKeyword = request.args.get(‘keyword’) or ‘load\_variables’  
resp= BuiltIn().run\_keyword(myKeyword)  
… Do some thing here with the resp  
return something

---

<div class="post-metadata">

**Author:** ![damies13](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/damies13/32/623_2.png) [@damies13](https://forum.robotframework.org/u/damies13)\
**Post date:** [10 March 2023 22:59 UTC](https://forum.robotframework.org/t/running-robot-framework-keywords-from-python-function/5428/2 "2023-03-10T22:59:16Z")

</div>

Hi Sarala,

The last chapter of the Robot Framework User Guide is [Internal API](https://robotframework.org/robotframework/latest/RobotFrameworkUserGuide.html#toc-entry-649), start buy having a read of the API documentation to get an idea of what you can do with it.

In you sample, I suspect the problem is you haven’t imported something else first that `robot.libraries.BuiltIn` needs? [Entry points](https://robot-framework.readthedocs.io/en/stable/index.html#entry-points) doesn’t list `robot.libraries` as an entry point, but i’m only guessing here.

Dave.

---

<div class="post-metadata">

**Author:** ![SKottapalli](https://avatars.discourse-cdn.com/v4/letter/s/9f8e36/32.png) [@SKottapalli](https://forum.robotframework.org/u/SKottapalli)\
**Post date:** [10 March 2023 23:07 UTC](https://forum.robotframework.org/t/running-robot-framework-keywords-from-python-function/5428/3 "2023-03-10T23:07:19Z")

</div>

I added below line before the run\_keyword. Still same error

```
      BuiltIn().import_resource('/myKeywords.robot')

```

Error: venv/lib/python3.9/site-packages/robot/libraries/BuiltIn.py", line 62, in \_get\_context  
raise RobotNotRunningError(‘Cannot access execution context’)  
robot.libraries.BuiltIn.RobotNotRunningError: Cannot access execution context

---

<div class="post-metadata">

**Author:** ![damies13](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/damies13/32/623_2.png) [@damies13](https://forum.robotframework.org/u/damies13)\
**Post date:** [10 March 2023 23:12 UTC](https://forum.robotframework.org/t/running-robot-framework-keywords-from-python-function/5428/4 "2023-03-10T23:12:14Z")

</div>

Hi Sarala,

From the error I suspect it’s failing at the line `from robot.libraries.BuiltIn import BuiltIn`, Unfortunately I’m not so familiar with the api, I’ve had a bit of a look at it, but moved on as there were better ways to achieve what i was trying to do

Dave.
