# Return variable value from Robot Framework to Python

**URL:** <https://forum.robotframework.org/t/return-variable-value-from-robot-framework-to-python/3857>\
**Category:** Python\
**Created:** [23 May 2022 14:51 UTC](https://forum.robotframework.org/t/return-variable-value-from-robot-framework-to-python/3857 "2022-05-23T14:51:39Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![erikapapp](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/erikapapp/32/1532_2.png) [@erikapapp](https://forum.robotframework.org/u/erikapapp)\
**Post date:** [23 May 2022 14:51 UTC](https://forum.robotframework.org/t/return-variable-value-from-robot-framework-to-python/3857/1 "2022-05-23T14:51:39Z")

</div>

Hi All,

I have a .py file that calls a.robot file during run.  
`robot.run("test.robot")`  
At the end of the test robot I have a variable ${output\_variable} with a calculated value and would like to give back this variable to the .py file.  
Could you please help how is this possible?  
`output_variable = BuiltIn.get_variable_value('${output_variable}')` is not working as I get the following error: `RobotNotRunningError('Cannot access execution context')`

Thanks

---

<div class="post-metadata">

**Author:** ![HelioGuilherme66](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/helioguilherme66/32/34_2.png) [@HelioGuilherme66](https://forum.robotframework.org/u/HelioGuilherme66)\
**Post date:** [23 May 2022 19:25 UTC](https://forum.robotframework.org/t/return-variable-value-from-robot-framework-to-python/3857/2 "2022-05-23T19:25:18Z")

</div>

Well, the error message says it all. Robot is no longer running!

This is what I would do:

- In your test Teardown, have some keyword to write the variable to a Python file. Then you can read it (import if python file, or read if some other format) (importing a file may have security questions)
- Maybe can be sent the value as an environment variable, that you can read after robot execution.

---

<div class="post-metadata">

**Author:** ![erikapapp](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/erikapapp/32/1532_2.png) [@erikapapp](https://forum.robotframework.org/u/erikapapp)\
**Post date:** [24 May 2022 08:24 UTC](https://forum.robotframework.org/t/return-variable-value-from-robot-framework-to-python/3857/3 "2022-05-24T08:24:16Z")

</div>

Thanks for the reply.  
When I call Python from Robot Framework it is very easy to pass and return variable values from Python functions, or using suite variable that Python can access.  
I thought there is an easy way to return variable values from Robot Framework when Python calls it, as pass variable to Robot Framework is very easy.

---

<div class="post-metadata">

**Author:** ![pekkaklarck](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/pekkaklarck/32/1019_2.png) [@pekkaklarck](https://forum.robotframework.org/u/pekkaklarck)\
**Post date:** [25 May 2022 12:56 UTC](https://forum.robotframework.org/t/return-variable-value-from-robot-framework-to-python/3857/4 "2022-05-25T12:56:27Z")

</div>

In addition to solutions already listed above, you could create a listener that stores this information. The listener could probably get the value of this variable in its `close` method and store it as an instance attribute that you can access after execution.

---

<div class="post-metadata">

**Author:** ![erikapapp](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/erikapapp/32/1532_2.png) [@erikapapp](https://forum.robotframework.org/u/erikapapp)\
**Post date:** [25 May 2022 13:30 UTC](https://forum.robotframework.org/t/return-variable-value-from-robot-framework-to-python/3857/5 "2022-05-25T13:30:50Z")

</div>

Thank you. Could you please help me out how to achieve it? Should I write it in Python and somehow call it?

---

<div class="post-metadata">

**Author:** ![pekkaklarck](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/pekkaklarck/32/1019_2.png) [@pekkaklarck](https://forum.robotframework.org/u/pekkaklarck)\
**Post date:** [25 May 2022 14:34 UTC](https://forum.robotframework.org/t/return-variable-value-from-robot-framework-to-python/3857/6 "2022-05-25T14:34:23Z")

</div>

Listener API is explained in the User Guide:  
[http://robotframework.org/robotframework/latest/RobotFrameworkUserGuide.html#listener-interface](http://robotframework.org/robotframework/latest/RobotFrameworkUserGuide.html#listener-interface)

You start execution programmatically and with listeners the usage could be something like this:

```python
listener = MyListener()
robot.run(..., listener=listener)
variable = listener.variable

```

---

<div class="post-metadata">

**Author:** ![erikapapp](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/erikapapp/32/1532_2.png) [@erikapapp](https://forum.robotframework.org/u/erikapapp)\
**Post date:** [26 May 2022 10:25 UTC](https://forum.robotframework.org/t/return-variable-value-from-robot-framework-to-python/3857/8 "2022-05-26T10:25:37Z")

</div>

I have created the `MyListener` class with a single close def.

```
def close(self):
	var = BuiltIn().get_variable_value("${output_variable}")
	return var

```

The `output_variable` value is set in the .robot file.

When I call the close `variable = listener.close()` in my python file I get the “Cannot access execution context” error.

---

<div class="post-metadata">

**Author:** ![pekkaklarck](https://dub1.discourse-cdn.com/flex005/user_avatar/forum.robotframework.org/pekkaklarck/32/1019_2.png) [@pekkaklarck](https://forum.robotframework.org/u/pekkaklarck)\
**Post date:** [27 May 2022 13:30 UTC](https://forum.robotframework.org/t/return-variable-value-from-robot-framework-to-python/3857/9 "2022-05-27T13:30:41Z")

</div>

That’s annoying. Apparently the execution context isn’t anymore available that late in the execution. What ought to work is capturing the value in the last `end_suite` method. To know you are in the last `end_suite`, you can either implement also `start_suite` to keep track how many suites have started or just that check is suite id `s1`.
