pythonrpywin32xlwingsreticulate

Reticulate with pywin32, a dependency of xlwings, not found when sourcing python script from R


Overview

I have a python script opens an excel workbook, makes some changes, then saves it. This is just one step in a series of excel processing operations, most of which are written in R. My end goal is to run all the operations from a single R script that looks roughly like:

library(reticulate)

source('step1.r')
source_python('step2.py')
source('step3.r)
...

Problem

So the python script works and runs just fine in Python. But when I attempt to source the script in R using reticulate, I keep encountering dependency errors.

Here is a minimum reproducible example: Python 3.11.1 script temp.py, running in Spyder. This works fine.

import xlwings as xw
ATL = xw.Book(r'C:\Users\myname\Documents\test.xlsx')
print("the end")

R script, using R version 4.4.0:

library(reticulate)
source_python(r"(C:\Users\myname\.spyder-py3\temp.py)") # this is the problem line

Which outputs:

Error in py_run_file_impl(file, local, convert) : 
  xlwings.XlwingsError: Make sure to have "pywin32", a dependency of xlwings, installed.

So of course, I open my terminal in RStudio and python -m pip install xlwings and get a final output message of

Successfully installed pywin32-308 xlwings-0.33.9

Just in case, I follow the XLwings Installation and run these two in the RStudio terminal: pip install "xlwings[all]" and xlwings addin install. Both complete their processes without issue.

But source_python(r"(C:\Users\myname\.spyder-py3\temp.py)") still gives the same error in R, claiming that I don't have pywin32 installed.

Other Things I Tried

Adding py_require('xlwings') before the source_python line doesn't make a difference.

I tried this answer's run_python_file function, which attempts to automatically install missing packages. Same error.

This reported issue on the xlwings git describes the problem, but for Python. Remember, my python script runs fine in Spyder IDE. My error occurs only when I try to source the python script from R using reticulate. But I gave this a shot: "If everything fails, using a miniconda distribution and installing it via conda install pywin32 is usually solving these issues." Did not work, my Anaconda Prompt (miniconda3) outputted # All requested packages already installed after leading me onto a wild goose chase setting up the SSL certificate.

Which brings me here. Any help is greatly appreciated. I am not attached to reticulate, so if there's a different R package that lets you source python scripts, that would be nice.


Solution

  • Sigh. Of course after I spend hours researching and writing up a stackoverflow question, I find the right answer hidden among other answers.

    RStudio > Tools > Global Options > Python > Select (your python interpreter) > Virtual Environments > ~/.virtualenvs/r-reticulate/Scripts/python.exe > Select > Apply

    Now source_python(r"(C:\Users\myname\.spyder-py3\temp.py)") works like a charm.

    [Edit] https://stackoverflow.com/a/68741989/20452011 helped when I had same issue with the openpyxl module not installed. reticulate::py_install("openpyxl") might've also worked for my original problem with pywin32, but who knows...