Jacqueline Nolis
Jacqueline Nolis

Reputation: 1547

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!

Upvotes: 2

Views: 870

Answers (1)

Jacqueline Nolis
Jacqueline Nolis

Reputation: 1547

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'")

Upvotes: 4

Related Questions