rstudio / tensorflow

TensorFlow for R
https://tensorflow.rstudio.com
Apache License 2.0
1.33k stars 318 forks source link

Installation Issue: 'r-tensorflow' exists but is not a virtual environment #584

Closed candelas762 closed 11 months ago

candelas762 commented 1 year ago

I saw that there is a similar posted issue ( #522 ) but I tried to follow it and it did not solve my error.

I also tried the suggested procedure when you open a new Installation Issue and still not fixed.

After running the first snippet:

# install the development version of packages, in case the
# issue is already fixed but not on CRAN yet.
install.packages("pak")
pak::pak(sprintf("rstudio/%s", c("reticulate", "tensorflow", "keras")))
if (is.null(reticulate::virtualenv_starter()))
   reticulate::install_python()
keras::install_keras()

I get the error at:

> keras::install_keras()
Error in virtualenv_install(envname = envname, packages = packages, ignore_installed = pip_ignore_installed,  : 
  'r-tensorflow' exists but is not a virtual environment

After running the second snippet I get:

> tensorflow::as_tensor("Hello World")
Error: Valid installation of TensorFlow not found.

Python environments searched for 'tensorflow' package:
 C:\Users\jaca\OneDrive - Norwegian University of Life Sciences\Documents\.virtualenvs\r-reticulate\Scripts\python.exe

Python exception encountered:
 Traceback (most recent call last):
  File "C:\Users\jaca\AppData\Local\R\win-library\4.2\reticulate\python\rpytools\loader.py", line 119, in _find_and_load_hook
    return _run_hook(name, _hook)
  File "C:\Users\jaca\AppData\Local\R\win-library\4.2\reticulate\python\rpytools\loader.py", line 93, in _run_hook
    module = hook()
  File "C:\Users\jaca\AppData\Local\R\win-library\4.2\reticulate\python\rpytools\loader.py", line 117, in _hook
    return _find_and_load(name, import_)
ModuleNotFoundError: No module named 'tensorflow'

You can install TensorFlow using the install_tensorflow() function.
In addition: Warning message:
In Sys.setlocale("LC_CTYPE", ctype) :
  OS reports request to set locale to "Norwegian Bokmål_Norway.utf8" cannot be honored

After running the third snippet I get:

> reticulate::py_config()
python:         C:/Users/jaca/OneDrive - Norwegian University of Life Sciences/Documents/.virtualenvs/r-reticulate/Scripts/python.exe
libpython:      C:/Users/jaca/AppData/Local/r-reticulate/r-reticulate/pyenv/pyenv-win/versions/3.9.13/python39.dll
pythonhome:     C:/Users/jaca/OneDrive - Norwegian University of Life Sciences/Documents/.virtualenvs/r-reticulate
version:        3.9.13 (tags/v3.9.13:6de2ca5, May 17 2022, 16:36:42) [MSC v.1929 64 bit (AMD64)]
Architecture:   64bit
numpy:          C:/Users/jaca/OneDrive - Norwegian University of Life Sciences/Documents/.virtualenvs/r-reticulate/Lib/site-packages/numpy
numpy_version:  1.26.0
tensorflow:     [NOT FOUND]
> tensorflow::tf_config()
Valid installation of TensorFlow not found.

Python environments searched for 'tensorflow' package:
 C:\Users\jaca\OneDrive - Norwegian University of Life Sciences\Documents\.virtualenvs\r-reticulate\Scripts\python.exe

Python exception encountered:
 Traceback (most recent call last):
  File "C:\Users\jaca\AppData\Local\R\win-library\4.2\reticulate\python\rpytools\loader.py", line 119, in _find_and_load_hook
    return _run_hook(name, _hook)
  File "C:\Users\jaca\AppData\Local\R\win-library\4.2\reticulate\python\rpytools\loader.py", line 93, in _run_hook
    module = hook()
  File "C:\Users\jaca\AppData\Local\R\win-library\4.2\reticulate\python\rpytools\loader.py", line 117, in _hook
    return _find_and_load(name, import_)
ModuleNotFoundError: No module named 'tensorflow'

You can install TensorFlow using the install_tensorflow() function.

> reticulate::import("tensorflow")
Error in py_module_import(module, convert = convert) : 
  ModuleNotFoundError: No module named 'tensorflow'
Run `reticulate::py_last_error()` for details.
> reticulate::py_last_error()

-- Python Exception Message ------------------------------------------------------------------------------------------
Traceback (most recent call last):
  File "C:\Users\jaca\AppData\Local\R\win-library\4.2\reticulate\python\rpytools\loader.py", line 119, in _find_and_load_hook
    return _run_hook(name, _hook)
  File "C:\Users\jaca\AppData\Local\R\win-library\4.2\reticulate\python\rpytools\loader.py", line 93, in _run_hook
    module = hook()
  File "C:\Users\jaca\AppData\Local\R\win-library\4.2\reticulate\python\rpytools\loader.py", line 117, in _hook
    return _find_and_load(name, import_)
ModuleNotFoundError: No module named 'tensorflow'

-- R Traceback -------------------------------------------------------------------------------------------------------
    x
 1. \-reticulate::import("tensorflow")
 2.   \-reticulate:::py_module_import(module, convert = convert)
> sessionInfo()
R version 4.2.2 (2022-10-31 ucrt)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows Server x64 (build 20348)

Matrix products: default

locale:
[1] C
system code page: 65001

attached base packages:
[1] stats     graphics  grDevices utils     datasets  methods   base     

loaded via a namespace (and not attached):
 [1] Rcpp_1.0.11            here_1.0.1             lattice_0.20-45        png_0.1-8             
 [5] pak_0.6.0              rprojroot_2.0.3        withr_2.5.1            zeallot_0.1.0         
 [9] rappdirs_0.3.3         grid_4.2.2             R6_2.5.1               lifecycle_1.0.3       
[13] jsonlite_1.8.7         magrittr_2.0.3         tfruns_1.5.1           rlang_1.1.1           
[17] cli_3.6.1              rstudioapi_0.15.0      whisker_0.4.1          Matrix_1.5-1          
[21] generics_0.1.3         reticulate_1.32.0.9002 keras_2.13.0.9000      tools_4.2.2           
[25] compiler_4.2.2         base64enc_0.1-3        tensorflow_2.14.0.9000
t-kalinowski commented 1 year ago

That error is raised if the virtualenv directory is missing certain files that indicate it is indeed a venv. In particular: here and here

keras::install_keras() should remove any pre-existing environment named "r-tensorflow", so something is likely going wrong w/ the venv creation and there is a good chance we'll see what's wrong in the output from install_keras(). Can you please copy-paste the full output from running install_keras()?

My suspicion is that this is due to the venv being on a OneDrive managed location, and that somehow interferes with file deletion or creation. I recommend changing the default location for virtualenvs by setting the envvar WORKON_HOME to a directory local to your machine. (either setting it system wide in system setting, or by placing something like this in .Renviron

WORKON_HOME=~/../.virtualenvs
chaosssss123 commented 11 months ago

I have got the same exactly problem:

Error in virtualenv_install(envname = envname, packages = packages, ignore_installed = pip_ignore_installed, : 'r-tensorflow' exists but is not a virtual environment

I have tried everything, installed anaconda and following exactly the steps in https://tensorflow.rstudio.com/install/index.html.

It simply does not work.

Any advice how to deal with this?

t-kalinowski commented 11 months ago

Please see the discussion in https://github.com/rstudio/keras/issues/1381

chaosssss123 commented 11 months ago

thanks. i tried this:

unlink(paste0(virtualenv_root(), "/r-tensorflow"), force = TRUE, recursive = TRUE)

then installed it again and i got this message:

rror in install_tensorflow(envname = "r-tensorflow") : You should call install_tensorflow()/install_keras() only in a fresh R session that has not yet initialized Keras and TensorFlow (this is to avoid DLL in use errors during installation)

i did this and then it indeed starts to install tensorflow, hwoever after a while R is kind of crahses, saying that a fatal; erros occured and asking me to restart a new session. I did it again, and I got the same exactly error message. I do not know what one arth is going on with this. Can you help?many thanks again

chaosssss123 commented 11 months ago

it crashes after :'Downloading tensorflow_intel-2.14.0-cp39-cp39-win_amd64.whl (284.1 MB)'

chaosssss123 commented 11 months ago

also anaconda navigator crashes after installation. i dont know whether this is related

chaosssss123 commented 11 months ago

this is the error message i get from failing to open anaconda navigator: 'the item 'pythonw.exe' that this shortcut refers to has been changed or moved, so this shortut...'

chaosssss123 commented 11 months ago

issue still unresolved. any advice?