r-bridge: Can't run python code in R from Quarto when arcgisbinding is loaded

I need to pull data from a web service using an API written for R, convert it to a fGDB, and use a python script to “overwrite” the data currently sitting in AGOL. So far I can get everything to work if I do it piece by piece, but trying to load arcgisbinding to create my fGDB in R, then switching to python code in another chunk, is not working.

Perhaps arcgisbinding and reticulate are using different versions of python? I don’t have experience with environments/python - I’ve been trying to troubleshoot this but finding it difficult to pick out what solution might work in my case.

Running on Windows 10 Enterprise (with admin privs) RStudio “Desert Sunflower” 2023.09.0 Build 463
64 bit R 4.2.3
arcgisbinding 1.0.1.305

This works - everything runs as expected (including the rest of the python code, which is not necessary for this minimal - hopefully reproducible - example). Running in a Quarto .qmd document.

---
title: "Which python version?"
format: html
editor: visual
---

```{r libraries}
library(reticulate)
use_python('C:\\Program Files\\ArcGIS\\Pro\\bin\\Python\\envs\\arcgispro-py3\\python.exe', required = T)
```

```{python}
import arcpy
```

But if I include arcgisbinding I get this error message after running my first line of python code:

---
title: "Which python version?"
format: html
editor: visual
---

```{r libraries}
library(reticulate)
use_python('C:\\Program Files\\ArcGIS\\Pro\\bin\\Python\\envs\\arcgispro-py3\\python.exe', required = T)
library(arcgisbinding)
arc.check_portal()
arc.check_product()
```

```{python}
import arcpy
```

ImportError: 

IMPORTANT: PLEASE READ THIS FOR ADVICE ON HOW TO SOLVE THIS ISSUE!

Importing the numpy C-extensions failed. This error can happen for
many reasons, often due to issues with your setup or how NumPy was
installed.

We have compiled some common reasons and troubleshooting tips at:

    https://numpy.org/devdocs/user/troubleshooting-importerror.html

Please note and check the following:

  * The Python version is: Python3.9 from "C:/Program Files/ArcGIS/Pro/bin/Python/envs/arcgispro-py3/python.exe"
  * The NumPy version is: "1.20.1"

and make sure that they are the versions you expect.
Please carefully study the documentation linked above for further help.

Original error was: DLL load failed while importing _multiarray_umath: The specified module could not be found.

If I try not specifying the python location, this is the error I get:

---
title: "Which python version?"
format: html
editor: visual
---

```{r libraries}
library(reticulate)
library(arcgisbinding)
arc.check_portal()
arc.check_product()
```

```{python}
import arcpy
```

> reticulate::repl_python()
Error: C:/Users/NewtonEr/AppData/Local/r-miniconda/envs/r-reticulate/python38.dll - The specified module could not be found.

About this issue

  • Original URL
  • State: closed
  • Created 9 months ago
  • Comments: 15 (7 by maintainers)

Most upvoted comments

Good morning @JosiahParry , thanks for that, I’ll give it a try.

I did finally get the python code working but it is extremely finicky. I have to load all my python libraries and connect to Pro before doing anything with arcgisbinding, then do my work in R, then call the libraries and connection back in again and run the python code. It’s not pretty, but it works. I don’t like it, though. I’m going to use your example and try to get that working but I guess I can fall back on this if I need to. For future reference, the below does not throw errors and successfully updates my feature class, using a version of ESRI’s truncate and append script.

  ---
  title: "Which python version?"
  format: html
  editor: source
  ---
  
  
  
  ```{r libraries}
  library(reticulate)
  use_python('C:\\Program Files\\ArcGIS\\Pro\\bin\\Python\\envs\\arcgispro-py3\\python.exe', required = T)
  ```
  
  ```{python}
  # You must load all your python libraries and connect to Pro here, before calling arcgisbinding, 
  # then load them again in your python code. Trust me. It took me DAYS to figure this out. 
  import arcpy
  import os, time, uuid
  from zipfile import ZipFile
  from arcgis.gis import GIS
  import arcgis.features
  
  # Create GIS object
  print("Connecting to AGOL")
  gis = GIS("Pro")
  ```
  
  
  
  ```{r}
  library(tidyverse)
  library(arcgisbinding)
  library(sf)
  arc.check_portal()
  arc.check_product()
  
  # Do stuff in R code like creating fGDBs 
  ```
  
  
  
  ```{python}
  import arcpy
  import os, time, uuid
  from zipfile import ZipFile
  from arcgis.gis import GIS
  import arcgis.features
  
  # Create GIS object
  print("Connecting to AGOL")
  gis = GIS("Pro")
  
  # Run the python code to update & append here...
  ```

@Nova-Scotia here’s a gist that walks through uploading an sf object as a feature service. It also updates fields and demonstrates how to delete every feature and add features again to the same feature service. There is an existing limitation, though, that for publish_layer() the CRS can only be 3857 right now. This is a bug I need to work out. However, if the feature service already exists and the CRS has been set you can use that one. The issue is in creating one with a different CRS. Feel free to start a discussion in arcgislayers https://github.com/R-ArcGIS/arcgislayers/discussions or email me directly jparry at esri dot com.

Regarding this python issue, I don’t know if I can help much further without having a quarto file i can try and reproduce the issue with 😦