r-spatial / rgee

Google Earth Engine for R
https://r-spatial.github.io/rgee/
Other
670 stars 146 forks source link

Earth Engine client library not initialized #370

Open bholtdwyer opened 1 month ago

bholtdwyer commented 1 month ago

At submit an issue, please attached the following information of your rgee session:

Returns FALSE. There is no ".config" folder in ~, which maps to C:/Users/holtd/OneDrive/Documents

P.S. I hate that Microsoft has sneakily mapped ~ to the OneDrive folder; I never asked for this.

There is such a credentials file in C:/Users/holtd/.config/earthengine

file.exists("C:/Users/holtd/.config/earthengine/credentials [1] TRUE

- [ ] You can run a simple EE command from R: 

```r
library(rgee)

# Initialize the Earth Engine module.
ee_Initialize()
── rgee 1.1.7 ──────────────────────────────────────────────────────────────────────────────────────────────────────────── earthengine-api 0.1.415 ── 
 ✔ user: not_defined 
 ✔ Initializing Google Earth Engine:  DONE!
Error in value[[3L]](cond) : 
  It looks like your EE credential has expired. Try running ee_Authenticate() again or clean your credentials ee_clean_user_credentials().

ee_Authenticate()
> ee_Authenticate()
 ✔ Initializing Google Earth Engine:
Enter verification code: xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
To authorize access needed by Earth Engine, open the following URL in a web browser and follow the instructions. If the web browser does not start automatically, please manually browse the URL below.

    https://code.earthengine.google.com/client-auth?scopes=https%3A//www.googleapis.com/auth/earthengine%20https%3A//www.googleapis.com/auth/cloud-platform%20https%3A//www.googleapis.com/auth/devstorage.full_control&request_id=e11tIX59KasISdm_IqvrUhWlScG00PE4X6w1uLBtRoY&tc=LTqXfxkUn7S9iwXp3Q_uiThs-On8JQi1jHNA0MdUfcg&cc=gExeK6K45XuAqeoEzLxv0mIgDDQCTz44C4Ai1LzSRww

The authorization workflow will generate a code, which you should paste in the box below.
 ✔ Initializing Google Earth Engine:  DONE!
credentials are cached in the path: C:\Users\holtd/.config/earthengine/

Successfully saved authorization token.

# Print metadata for a DEM dataset.
print(ee$Image('USGS/SRTMGL1_003')$getInfo())
Error in py_call_impl(callable, call_args$unnamed, call_args$named) : 
  ee.ee_exception.EEException: Earth Engine client library not initialized. See http://goo.gle/ee-auth.
#There is no such website.
stop(structure(list(message = "ee.ee_exception.EEException: Earth Engine client library not initialized. See http://goo.gle/ee-auth.\n\033[90mRun \033]8;;rstudio:run:reticulate::py_last_error()\a`reticulate::py_last_error()`\033]8;;\a for details.\033[39m",
call = py_call_impl(callable, call_args$unnamed, call_args$named)), class = c("ee.ee_exception.EEException",
"python.builtin.Exception", "python.builtin.BaseException", "python.builtin.object",
"error", "condition"), py_object = <environment>))
ee$Image("USGS/SRTMGL1_003")

Attach your Python (reticulate) configuration:

library(reticulate)
py_config()
python:         C:/Users/holtd/anaconda3/envs/rgee/python.exe
libpython:      C:/Users/holtd/anaconda3/envs/rgee/python38.dll
pythonhome:     C:/Users/holtd/anaconda3/envs/rgee
version:        3.8.19 | packaged by conda-forge | (default, Mar 20 2024, 12:38:07) [MSC v.1929 64 bit (AMD64)]
Architecture:   64bit
numpy:          C:/Users/holtd/anaconda3/envs/rgee/Lib/site-packages/numpy
numpy_version:  1.24.4
ee:             C:\Users\holtd\ANACON~1\envs\rgee\lib\site-packages\ee\__init__.p

NOTE: Python version was forced by RETICULATE_PYTHON

Description

I'm just trying to get some NDVI data pulled from Google Earth Engine, but I can't even get the library to work. Every time I try to access data, it keeps complaining that the Earth Engine Client Library is not initialized (whatever that means). As you can see from the above, the library tries to cache my authorization token in one location (C;/Users/holtd/.config) but then apparently searches for it in a different one ("~/.config", which is C:/Users/holtd/OneDrive/Documents/.config, which doesn't exist). I've tried copying the config file over from where it's created to where the library seems to be looking for it, but that doesn't solve the problem.

What I Did

#install.packages("rgee")
library(rgee)
require(reticulate)

# Manually installed Google Cloud SDK

HOME <- Sys.getenv("HOME")

## Set key paths
Sys.setenv("RETICULATE_PYTHON" = sprintf("%s/.local/share/r-miniconda/bin/python3", HOME))
Sys.setenv("EARTHENGINE_GCLOUD" = sprintf("%s/google-cloud-sdk/bin/", HOME))

#Install rgee Python dependencies
#ee_install()
##Asks to install Miniconda, I select Yes.
#After a lot of details prints:
#* Miniconda has been successfully installed at "C:/Users/holtd/AppData/Local/r-miniconda".
#Miniconda was successfully installed, please restart R and run again rgee::ee_install
#[1] TRUE

#I restart my R session as instructed
require(reticulate)
require(rgee)

rgee::ee_install()
#Tells me I have no Python on my system, even though it JUST INSTALLED miniconda. Okay.
rgee::ee_clean_pyenv()
rgee::ee_install_set_pyenv("C:/Users/holtd/AppData/Local/Microsoft/WindowsApps/python.exe")

EARTHENGINE_PYTHON='C:/Users/holtd/AppData/Local/Microsoft/WindowsApps/python.exe'
saved in: C:\Users\holtd\OneDrive\Documents/.Renviron
Do you want restart your R session? Windows users could need to terminate R instead of restarting. 

1: yes
2: no

Selection: 1
Warning message:
In ee_install_set_pyenv_env(py_env = py_env, py_path = python_path,  :
  Windows users must install miniconda/anaconda to use rgee. The use of a Python environment is mandatory.

#Yeah we just installed miniconda above. Not sure why rgee thinks there isn't any.
#Manually restart R, again.

require(reticulate)
require(rgee)
> ee_install()
reticulate did not find any Python ENV on your system.

Would you like to download and install Miniconda?
Miniconda is an open source environment management system for Python.
See https://docs.conda.io/en/latest/miniconda.html for more details.
Windows users must install miniconda/anaconda to use rgee!

If you think it is an error since you know you have a Python environment
in your system. Run as follow to solve:
- Using the rgee API:
1. rgee::ee_clean_pyenv()
2. rgee::ee_install_set_pyenv(py_path = "YOUR_PYTHON_PATH_GOES_HERE")
3. Restart your system.
- Using Rstudio 1.4:
   https://github.com/r-spatial/rgee/tree/help/rstudio/
Would you like to install Miniconda? [Y/n]: Y
Error: Miniconda is already installed at path "C:/Users/holtd/AppData/Local/r-miniconda".
- Use `reticulate::install_miniconda(force = TRUE)` to overwrite the previous installation.
- 
#Okay, so first I get an error saying there's no Python ENV, then when I try to install Miniconda, it manages to find it!

ee_check()
Error in ee_check_python(quiet = quiet) : 
  Unable to find a Python version, you will need to fix before run rgee::ee_Initialize(). For more details run reticulate::py_available()

rgee::ee_Initialize()
Error in reticulate::py_discover_config() : 
  Python specified in RETICULATE_PYTHON (C:/Users/holtd/AppData/Local/Microsoft/WindowsApps/python.exe) does not exist

HOME <- Sys.getenv("HOME")
## Set key paths
Sys.setenv("RETICULATE_PYTHON" = sprintf("%s/.local/share/r-miniconda/bin/python3", HOME))
Sys.setenv("EARTHENGINE_GCLOUD" = sprintf("%s/google-cloud-sdk/bin/", HOME))

ee_Initialize(verbose = F)
Error in reticulate::py_discover_config() : 
  Python specified in RETICULATE_PYTHON (C:/Users/holtd/AppData/Local/Microsoft/WindowsApps/python.exe) does not exist

#Didn't I literally *just* set an environment variable to point this to miniconda???

Sys.getenv("RETICULATE_PYTHON")
[1] "C:\\Users\\holtd\\OneDrive\\Documents/.local/share/r-miniconda/bin/python3"

#Okay, reticulate_python is pointing to this ^^ folder...which doesn't exist!!
#Nor is there any .local folder in C:/Users/holtd

ee_Initialize(verbose = F)
Error in reticulate::py_discover_config() : 
  Python specified in RETICULATE_PYTHON (C:/Users/holtd/AppData/Local/Microsoft/WindowsApps/python.exe) does not exist**

#Well, this file does exist, so I'm not sure what rgee is talking about.

ee_check()
Error in ee_check_python(quiet = quiet) : 
  Unable to find a Python version, you will need to fix before run rgee::ee_Initialize(). For more details run reticulate::py_available()

reticulate::py_available()
[1] FALSE

#Trying to manually override reticulate python
rgee::ee_clean_pyenv()
rgee::ee_install_set_pyenv("C:/Users/holtd/anaconda3/envs/rgee/python.exe")
EARTHENGINE_PYTHON='C:/Users/holtd/anaconda3/envs/rgee/python.exe'
saved in: C:\Users\holtd\OneDrive\Documents/.Renviron
Do you want restart your R session? Windows users could need to terminate R instead of restarting. 

1: yes
2: no

Selection: 2
Restart R session to see changes.
Warning message:
In ee_install_set_pyenv_env(py_env = py_env, py_path = python_path,  :
  Windows users must install miniconda/anaconda to use rgee. The use of a Python environment is mandatory.

Restarting R session...

# Manually restart R again

require(reticulate)
py_available()
[1] FALSE

#Alright, I give up.
ambarja commented 1 month ago

@bholtdwyer Please use version 0.1.3.70 of the Earth Engine API. Currently, rgee encounters errors with the latest version of the API. To upgrade the Earth Engine API, use the following function: :point_down:
FInally, this issue is similar to #63

ee_install_upgrade(version = "0.1.370",earthengine_env = "C:/Users/holtd/anaconda3/envs/rgee/python.exe")
fpirotti commented 1 month ago

The following helped me - Run this to authenticate reticulate::py_run_string("import ee; ee.Authenticate()") Run this to initialize... now GEE wants projects so you have to define a project - make sure you upgrade to latest GEE API (earthengine-api 0.1.416 ) - projects can be a number or ID (e.g. user/XXXX/projectname) reticulate::py_run_string("import ee; ee.Initialize(project='309736351492')")

hope it helps

ambarja commented 1 month ago

@fpirotti please use earthengine version 0.1.3.70. To update the version just run the following command, just keep in mind your earthengine_env

ee_install_upgrade(version = "0.1.370",earthengine_env = "C:/Users/holtd/anaconda3/envs/rgee/python.exe")