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?)