Reticulate, source_python and exec problems

I started to use reticulate to integrate R and python and I think it is great. Unfortunately I am having a problem and I think it is probably something I am doing wrong.

I have a R script

library(reticulate)
source_python("./my_python_script.py")

in my_python_script.py (python 3*) I am calling other scripts using
exec(open(my_other_python_script.py).read())

I get the error: NameError: name 'xxx' is not defined.
'xxx' is changing, sometimes it is at the beginning of the script, but if I am running some lines of the script using repl_python(), then the error is moving down the line(s).

To me it seems it does not always parse the whole file when exec is used. Is this a possibility? any workaround, beside rewriting the code to source everything from R instead of using exec into python scripts?

Thanks a lot and please let me know if you need further information

Laura

This is hard to offer much help with without a bit more insight into the actual python script you are executing and a bit more about your environment. First, I would be sure that the Python version you are calling here is the one you are expecting. If you do not specify the path to python, reticulate will guess for you. You can check with something like the following at the start of your script.

reticulate::py_discover_config()

If you are executing the right version of python, then I would start paring down your Python script, comment out a couple of exec calls, add some logging, etc. This will help get a better sense for what is happening, if the execution is happening in the way that you expect, and where the error is occurring.

Also, this confuses me. I don't quite understand what you're getting at here. Narrowing down where in your python file the error is happening will probably help discussion about the problem.

'xxx' is changing, sometimes it is at the beginning of the script, but if I am running some lines of the script using repl_python(), then the error is moving down the line(s).

You might take a look at this article. It may be that your python code needs some love. Does the python script run fine from the terminal? (i.e. calling python directly?)

1 Like

Thank you very much for your answer and apologies for not providing enough details. I am using conda environments and they are fully defined in the R code.

library(reticulate)
use_condaenv("my_env", conda="/full/path/to/conda", required = TRUE)

The code runs fine from both the python terminal (using exec to source other python scripts) and using repl_python(), again using exec to source other python scripts.

I think I agree my python code lacks love. I managed to get around some of the problems cleaning and re-structuring the python script. So it is good that reticulate found the problems.

Thanks

Laura

1 Like

I think this might be a result of a bug in the way we sourced Python files. Could you try installing the development version of reticulate (devtools::install_github("rstudio/reticulate")) to see if that fixes the problem? If it doesn't please post full source code of a Python script which exhibits the problem and we can take it from there.

2 Likes

Using the development version fixed the problem.
Thank you very much

1 Like