Installing Python packages into a virtualenv is not supported on Windows | Tensorflow & Keras for Windows 10

Viewed 2729

I'm trying to set up for the first time R environment with Keras and Tensorflow installed for Windows 10. This error shows in the RStudio but I tried also to do it from the Anaconda prompt in some other way and even if there's no error I'm not able to import Tensorflow properly. In RStudio:

> library(keras)
> install_keras(method = "conda", tensorflow = "gpu")
> Error: Installing Python packages into a virtualenv is not supported on Windows

In Anaconda prompt after "conda install -c conda-forge tensorflow" and "pip install --upgrade tensorflow-gpu":

 (base) PS C:\Users\userx> conda activate renv                        
   (renv) PS C:\Users\userx> python                                     
   Python 3.7.1 (default, Oct 28 2018, 08:39:03) [MSC v.1912 64 bit
   (AMD64)] :: Anaconda, Inc. on win32                                  
   Type "help", "copyright", "credits" or "license" for more
   information.                                                         
    import tensorflow as tf                                                                                                                                                                                                                  Traceback (most recent call last):                                   
   File
   "A:\Programy\tools\anaconda3\envs\renv\lib\site-packages\tensorflow\python\pywrap_tensorflow.py",
   line 58, in <module>                                                 
   from tensorflow.python.pywrap_tensorflow_internal import *           
   File
   "A:\Programy\tools\anaconda3\envs\renv\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py",
   line 28, in <module>                                                 
   _pywrap_tensorflow_internal = swig_import_helper()                                                                                                                                                                                         File
   "A:\Programy\tools\anaconda3\envs\renv\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py",
   line 24, in swig_import_helper                                       
   _mod = imp.load_module('_pywrap_tensorflow_internal', fp, pathname, description)                                                         
   File "A:\Programy\tools\anaconda3\envs\renv\lib\imp.py", line 242, in
   load_module                                                          
   return load_dynamic(name, filename, file)                            
   File "A:\Programy\tools\anaconda3\envs\renv\lib\imp.py", line 342, in
   load_dynamic                                                         
   return _load(spec)                                                   
   ImportError: DLL load failed: A dynamic link library (DLL)
   initialization routine failed.                                       
   During handling of the above exception, another exception occurred:  
   Traceback (most recent call last):                                   
   File "<stdin>", line 1, in <module>                                  
   File
   "A:\Programy\tools\anaconda3\envs\renv\lib\site-packages\tensorflow\__init__.py",
   line 24, in <module>                                                 
   from tensorflow.python import pywrap_tensorflow  # pylint:
   disable=unused-import                                                
   File
   "A:\Programy\tools\anaconda3\envs\renv\lib\site-packages\tensorflow\python\__init__.py",
   line 49, in <module>                                                 
   from tensorflow.python import pywrap_tensorflow                      
   File
   "A:\Programy\tools\anaconda3\envs\renv\lib\site-packages\tensorflow\python\pywrap_tensorflow.py",
   line 74, in <module>                                                 
   raise ImportError(msg)                                               
   ImportError: Traceback (most recent call last):                      
   File
   "A:\Programy\tools\anaconda3\envs\renv\lib\site-packages\tensorflow\python\pywrap_tensorflow.py",
   line 58, in <module>                                                 
   from tensorflow.python.pywrap_tensorflow_internal import *           
   File
   "A:\Programy\tools\anaconda3\envs\renv\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py",
   line 28, in <module>                                                 
   _pywrap_tensorflow_internal = swig_import_helper()                                                                                                                                                                                         File
   "A:\Programy\tools\anaconda3\envs\renv\lib\site-packages\tensorflow\python\pywrap_tensorflow_internal.py",
   line 24, in swig_import_helper                                       
   _mod = imp.load_module('_pywrap_tensorflow_internal', fp, pathname, description)                                                         
   File "A:\Programy\tools\anaconda3\envs\renv\lib\imp.py", line 242, in
   load_module                                                          
   return load_dynamic(name, filename, file)                            
   File "A:\Programy\tools\anaconda3\envs\renv\lib\imp.py", line 342, in
   load_dynamic                                                         
   return _load(spec)                                                   
   ImportError: DLL load failed: A dynamic link library (DLL)
   initialization routine failed.                                       
   Failed to load the native TensorFlow runtime.                        
   See https://www.tensorflow.org/install/errors                        
   for some common reasons and solutions.  Include the entire stack
   trace                                                                
   above this error message when asking for help.             

Any advice would be appreciated.

2 Answers

I also had a lot of problems trying to install keras and tensorflow in R but somehow, I managed to do it after 5 days of trial-and-error.

I had to install them in a notebook with Windows 7 Professional. The notebook was shared with other people so, I was not allowed to install Windows 10.

  1. Because of frequent failures, I decided to uninstall everything: Rtools, RStudio, Anaconda, and R. Thus, I could start from scratch.

  2. I searched for some remaining folders that needed to be deleted manually. Most persisted in the “C:/Users/Username/”, “C:/Users/Username/Documents”, and “C:/Users/Username/AppData/Local”. Once I found the folder “r-reticulate” created by miniconda when I tried using it. That could be the reason why I was unsuccessful that time.

  3. I reset my notebook

  4. reinstalled the most recent versions of R (4.0.2) and RStudio (1.3.959)

  5. reinstalled the most recent version of rtools (40)

  6. Close and reopen RStudio if it is open

  7. I followed the recommended steps detailed in the rtools page:

7.1 inside RStudio, type in the console panel:

writeLines('PATH="${RTOOLS40_HOME}\usr\bin;${PATH}"', con="~/.Renviron")

7.2 start a new session in R

7.3 type in the console panel:

Sys.which("make")

7.4 something like this is printed if everything is ok:

"C:\rtools40\usr\bin\make.exe"

7.5 you can close RStudio

  1. I installed the most recent version of Anaconda 3 (even though it was not really recommended for Windows 7 users)

  2. Open the “Anaconda Prompt”

9.1 I created a new environment named “r-reticulate” that will use a previous version of Python by typing:

conda create --name r-reticulate python=3.6

9.2 check if everything is ok by activating it with:

activate r-reticulate

9.3 the prompt should have changed

9.4 check the existing environments with:

conda info --envs

9.5 the “r-reticulate” environments should be indicated with an “*”

9.6 I closed Anaconda Prompt

  1. I reopened RStudio and entered:

install.packages(“remotes”)

remotes::install_github(“rstudio/keras”, dependencies = TRUE)

  1. Started a new session and entered:

library(keras)

library(reticulate)

use_condaenv("r-reticulate", required = TRUE)

install_keras(method = "conda", tensorflow = "1.13.1")

  1. Note I used a previous version of tensorflow. Some users had problems using the most recent version

13 If you were successful you can test keras with:

library(keras)

mnist <- dataset_mnist()

13.3 This should load the mnist data set

14 You can test tensorflow with:

library(tensorflow)

tf$constant("Hellow Tensorflow")

14.3 You should receive the output:

Tensor("Const:0", shape=(), dtype=string)

Well, I hope this helps you. No isolated solution in the web worked for me.

I had the same issue, tried many solutions (including the one reported here by mutiumi on which is based my own, ie: Anaconda packages selected for the installation will be the one R reported to me in an error as missing packages it cannot be able to install when I followed their procedure) without success.

My final working solution relay on installing all the necessary dependencies from Anaconda Navigator directly:

Prerequisites

  • set up and check all the Nvidia/CUDA/cuDNN stuff (spoiler: it will be quite frustrating!)

  • Install the very base dependencies (R4.0.2, RStudio latest (I personally use the preview version), Rtools 40, Anaconda3)

Python TF and Keras environments

  • Open Anaconda Navigator
  • Create a new "r-reticulate" environment (deleting it if you have already one with that name because a previous installation)
  • right-click on the python package listed on the right box of the window and select to change the version to 3.7.7 (the dependencies required are not currently supported for python 3.8+, hence I have selected the last supported one :-))
  • show "not-installed", click on "update index...", and select and install (in order):
    • pillow
    • requests
    • pyyaml
    • h5py
    • tensorflow-gpu
    • tensorflow-hub
    • keras-gpu

Setup and test on R

  • install.packages("keras", dependencies = TRUE)
  • library(keras)
    library(tensorflow)
    
    # This should load the mnist data set
    # AKA: check Keras is 0K
    mnist <- dataset_mnist()
    
    # You should receive the output:
    #     Tensor("Const:0", shape=(), dtype=string)
    #
    # Moreover, you should see in the starting messages
    # that your GPU is correctly detected, and ready to be used,
    # for me (somewhere in the middle):
    # "
    #   Found device 0 with properties:
    #   pciBusID: 0000:01:00.0 name: Quadro RTX 5000 computeCapability: 7.5
    # "
    #
    # AKA: check TF-gpu is 0K
    tf$constant("Hellow Tensorflow")
    
Related