# Call Robot Keywords from \*.py file and run it with Pytest

**URL:** <https://forum.robotframework.org/t/call-robot-keywords-from-py-file-and-run-it-with-pytest/2024>\
**Category:** Python\
**Created:** [30 June 2021 07:27 UTC](https://forum.robotframework.org/t/call-robot-keywords-from-py-file-and-run-it-with-pytest/2024 "2021-06-30T07:27:53Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![SenthilnathanS](https://avatars.discourse-cdn.com/v4/letter/s/7ab992/32.png) [@SenthilnathanS](https://forum.robotframework.org/u/SenthilnathanS)\
**Post date:** [30 June 2021 07:27 UTC](https://forum.robotframework.org/t/call-robot-keywords-from-py-file-and-run-it-with-pytest/2024/1 "2021-06-30T07:27:53Z")

</div>

Hi,  
I would like to know how to call a keywords from a robot file in \*.py file.  
And I would like to run the python script with pytest.

I tried with below script but receiving error.

from robot.libraries.BuiltIn import BuiltIn

def test\_capital\_case():  
BuiltIn().import\_resource(‘ValidLogin.robot’)  
#BuiltIn().run\_keyword(‘TC\_ValidLoginToTheApplication’)  
BuiltIn().call\_method(‘K\_LoginToTheApplication’)

Error Received:  
======================================================================== FAILURES ========================================================================  
\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_ test\_capital\_case \_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_\_

```
def test_capital_case():

```

> ```
> BuiltIn().import_resource('ValidLogin.robot')
> 
> ```

test\_example.py:8:

* * *

…\venv\lib\site-packages\robot\libraries\BuiltIn.py:2988: in import\_resource  
self.\_namespace.import\_resource(path)  
…\venv\lib\site-packages\robot\libraries\BuiltIn.py:74: in \_namespace  
return self.\_get\_context().namespace

* * *

self = \<robot.libraries.BuiltIn.BuiltIn object at 0x000001720CC50F40\>, top = False

```
def _get_context(self, top=False):
    ctx = EXECUTION_CONTEXTS.current if not top else EXECUTION_CONTEXTS.top
    if ctx is None:

```

> ```
> raise RobotNotRunningError('Cannot access execution context')
> 
> ```

E robot.libraries.BuiltIn.RobotNotRunningError: Cannot access execution context

…\venv\lib\site-packages\robot\libraries\BuiltIn.py:69: RobotNotRunningError  
================================================================ short test summary info =================================================================  
FAILED test\_example.py::test\_capital\_case - 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:** [2 July 2021 05:27 UTC](https://forum.robotframework.org/t/call-robot-keywords-from-py-file-and-run-it-with-pytest/2024/2 "2021-07-02T05:27:26Z")

</div>

Hi Senthilnathan,

I’ve never used this functionality, so my answer is based on what I read in the documentation for [module-robot.libraries.BuiltIn.import\_resource](https://robot-framework.readthedocs.io/en/stable/autodoc/robot.libraries.html#robot.libraries.BuiltIn.BuiltIn.import_resource)

> The given path must be absolute or found from search path. Forward slashes can be used as path separator regardless the operating system.

I’m guessing that your robot file is not found in the search path? so I suspect you just need to add the path before the robot file, see below:

```python
from robot.libraries.BuiltIn import BuiltIn

def test_capital_case():
    #BuiltIn().import_resource(‘ValidLogin.robot’)
    BuiltIn().import_resource(‘/path/to/ValidLogin.robot’)
    #BuiltIn().run_keyword(‘TC_ValidLoginToTheApplication’)
    BuiltIn().call_method(‘K_LoginToTheApplication’)

```

I would try this first, and at least rule it out, if you don’t want paths hard coded in your code (totally understandable), then you can use a python variable for the path and pass that in or you can investigate why the robot file is not in the search path.

Hope this helps,

Dave.

---

<div class="post-metadata">

**Author:** ![DetachHead](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/detachhead/32/3010_2.png) [@DetachHead](https://forum.robotframework.org/u/DetachHead)\
**Post date:** [11 January 2024 22:38 UTC](https://forum.robotframework.org/t/call-robot-keywords-from-py-file-and-run-it-with-pytest/2024/3 "2024-01-11T22:38:13Z")

</div>

you can use [pytest-robotframework](https://github.com/detachhead/pytest-robotframework) for this, it will run your test with robot so the execution context will be available.
