Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using environmental variables with R reticulate

I have a Python package that I want to use in R via reticulate. However, that Python function doesn't appear to see the environmental variables from the R environment. How can I successfully set an environmental variable for the Python function to see?

So if I had a python function like:

import os

def: toy_function():
    return os.environ['ENVVAR']

I would like to be able to do:

library(reticulate)
source_python("toy_function.py")
sys.setenv("ENVVAR"="HELLO")
print(toy_function())

And see "HELLO". Currently I am getting an error that "ENVVAR" cannot be found.

Thank you!

like image 802
Jacqueline Nolis Avatar asked Oct 15 '25 14:10

Jacqueline Nolis


1 Answers

Oh, it turns out there is a strange workaround for this, where you just need to call the environmental variable setting directly in Python from R:

py_run_string("import os")
py_run_string("os.environ['ENNVAR'] = 'HELLO'")
like image 100
Jacqueline Nolis Avatar answered Oct 18 '25 19:10

Jacqueline Nolis



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!