Installing python tensorflow with Rstudio

I have Rstudio version 2024.12.1 Build 563 installed. I have theR packages tensorflow and keras installed. I would like to work with python code for tensorflow and keras. I am trying to get started with python code using Rstudio.
I am not able to install the python packages tensorflow and keras. I show below my attempts:

#library(keras)
#library(tensorflow)
library(reticulate) # No error messages
py_config()
python: C:/AnacondaF/python.exe
libpython: C:/AnacondaF/python312.dll
pythonhome: C:/AnacondaF
version: 3.12.7 | packaged by Anaconda, Inc. | (main, Oct 4 2024, 13:17:27) [MSC v.1929 64 bit (AMD64)]
Architecture: 64bit
numpy: C:/AnacondaF/Lib/site-packages/numpy
numpy_version: 1.26.4

NOTE: Python version was forced by RETICULATE_PYTHON

path_to_python <- "C:/AnacondaF/python.exe"
virtualenv_create("r-reticulate", python = path_to_python)
virtualenv: r-reticulate
install_tensorflow(envname = "r-reticulate")
Error in install_tensorflow(envname = "r-reticulate") :
could not find function "install_tensorflow"
py_install("tensorflow",envname="r-reticulate",method="conda")

  • "C:/AnacondaF/condabin/conda.bat" install --yes --name r-reticulate -c conda-forge tensorflow
    Channels:
  • conda-forge
  • pymks
  • anaconda-fusion
  • defaults
    Platform: win-64
    Collecting package metadata (repodata.json): ...working... done
    Solving environment: ...working... warning libmamba Added empty dependency for problem type SOLVER_RULE_UPDATE
    failed

LibMambaUnsatisfiableError: Encountered problems while solving:

  • package tensorflow-1.7.0-0 requires tensorflow-base 1.7.0, but none of the providers can be installed

Could not solve for environment specs
The following packages are incompatible
โ”œโ”€ pin-1 is installable and it requires
โ”‚ โ””โ”€ python 3.13.* , which can be installed;
โ””โ”€ tensorflow is not installable because there are no viable options
โ”œโ”€ tensorflow [0.12.1|1.0.0|...|1.9.0] would require
โ”‚ โ””โ”€ python [3.5* |3.5.* |>=3.5,<3.6.0a0 ], which conflicts with any installable versions previously reported;
โ”œโ”€ tensorflow [1.10.0|1.13.1|...|1.9.0] would require
โ”‚ โ”œโ”€ python >=3.6,<3.7.0a0 , which conflicts with any installable versions previously reported;
โ”‚ โ””โ”€ tensorflow-base [1.13.1 py36_4|1.13.1 py36_5|...|1.14.0 py36h9f0ad1d_0], which requires
โ”‚ โ””โ”€ python >=3.6,<3.7.0a0 , which conflicts with any installable versions previously reported;
โ”œโ”€ tensorflow [1.13.1|1.14.0|...|2.9.1] would require
โ”‚ โ”œโ”€ python [3.7.* |>=3.7,<3.8.0a0 ], which conflicts with any installable versions previously reported;
โ”‚ โ””โ”€ tensorflow-base [1.13.1 py37_7|1.14.0 py37hc8dfbb8_0], which requires
โ”‚ โ””โ”€ python >=3.7,<3.8.0a0 , which conflicts with any installable versions previously reported;
โ”œโ”€ tensorflow [1.10.0|1.11.0|...|2.1.0] would require
โ”‚ โ””โ”€ python [3.6* |3.6.* ], which conflicts with any installable versions previously reported;
โ”œโ”€ tensorflow [1.7.0|1.7.1|1.8.0] would require
โ”‚ โ””โ”€ tensorflow-base [1.7.0 |1.7.1 |1.8.0 ] but there are no viable options
โ”‚ โ”œโ”€ tensorflow-base [1.7.0|1.7.1|1.8.0] would require
โ”‚ โ”‚ โ””โ”€ python >=3.5,<3.6.0a0 , which conflicts with any installable versions previously reported;
โ”‚ โ””โ”€ tensorflow-base [1.13.1|1.13.2|...|1.8.0], which cannot be installed (as previously explained);
โ”œโ”€ tensorflow [2.10.0|2.8.2|2.9.1] would require
โ”‚ โ””โ”€ python 3.10.* , which conflicts with any installable versions previously reported;
โ”œโ”€ tensorflow [2.10.0|2.3.0|...|2.9.1] would require
โ”‚ โ””โ”€ python 3.8.* , which conflicts with any installable versions previously reported;
โ””โ”€ tensorflow [2.10.0|2.5.0|2.6.0|2.8.2|2.9.1] would require
โ””โ”€ python 3.9.* , which conflicts with any installable versions previously reported.

Pins seem to be involved in the conflict. Currently pinned specs:

  • python 3.13.* (labeled as 'pin-1')

Error: one or more Python packages failed to install [error code 1]

Why am I not able to install the python packages? Please advise.

Your pinned Python version (3.13) is not compatible, current latest TF-2.19 requires Python 3.9โ€“3.12 and for Windows builds of TensorFlow conda packages you'd need to go back to at least Python 3.10

  • from conda-forge you'd get tensorflow[1.14.0] that requites python [3.7.* ]
  • latest from main channel is tensorflow [2.10.0], requiring python [3.10.* ] (there are also builds for 3.8 & 3.9).

Is Anaconda (or conda in general) one of your requirements? Current reticulate version prefers uv-backed ephemeral Python environments over conda / venv . If this sounds like an option, you might find Installation Issue - no valid installation of tensorflow ยท Issue #622 ยท rstudio/tensorflow ยท GitHub to be helpful (remove environments created by reticulate , install tensorflow (the R package) from github and set release date constraints for pip packages -- some people (incl. me) are having issues with TF-2.19 on Windows)

Thanks for your response. They are very helpful. However, I must make it clear that I am a beginner when it comes to tensorflow, python and Rstudio. I have the following questions:

  1. In response to your question about Anaconda, I will say that I do not have any specific requirements. I am prepared to do what it takes to have a smooth running Rstudio, python and its packages that includes tensorflow.

  2. You say that TF-2.19 is not compatible with Python 3.13.*. So, what are my options? Shall I install a previous version of python? Which one?

  3. You say that reticulate prefers uv- backed ephemeral Python environments? I am afraid that I do not follow. You seem to imply that conda/venv is not preferred. What does this mean in concrete terms? Shall I uninstall Anaconda and go for an alternative? I am prepared to do this if I get some handholding advice on what I should do. Or, is there some safe option with Anaconda (with a previous version of Python that is compatible with TF)?

  4. You seem to say that it is better to avoid virtual environments created by reticulate. Is this a general comment on virtual environments, or just those created with reticulate?

  5. I am working on windows. I have tensorflow already installed. Is it necessary to specifically install from github?

  6. You require that I set release date constraints for pip packages. Please let me know how I can do this.

  7. Your suggestion - latest from main channel is tensorflow [2.10.0], requiring python [3.10.* ]. How do I specify main channel?

  8. As you can see, I am totally out of my depth here. I would appreciate it if you can help me with basic step-by-step instructions.

Thanks a lot for your help.

Ephemeral Python environments in reticulate are relatively new, introduced in release 1.41.0 (2025-02-24). Preferred method to declare Python version & package requirements is now through py_require() calls in R code and for this to work, you can't use conda or virtualenv environments. There could be a number of perfectly valid cases where you do need to / want to stick with virtualenv or conda ( or mamba or micromamba), e.g. you need that specific TensorFlow 2.10 build from year 2022, but if not quite sure, I'd just go with py_require() & uv-controlled ephemeral environments.

R packages that are using reticulate also need to be updated to use py_require() to be compatible with ephemeral environments, tensorflow( the R package) CRAN release is from 2024-04-15, so you'd need up to date version from GitHub . Besides, TensorFlow for R - Quick start also guides you to install from GitHub and not from CRAN.


As guided in previously linked GithHub issue, start by updating tensorflow R package:

# install.packages("remotes")
remotes::install_github("rstudio/tensorflow")

Then force ephemeral environment use through RETICULATE_PYTHON environment variable and set release date constraints for pip packages. Python version and package dependencies will be resolved when loading tensorflow package, so first run might take few moments as uv needs to fetch Python binaries and all tensorflow (the pip package) dependencies.

Sys.setenv("RETICULATE_PYTHON" = "managed")
reticulate::py_require(exclude_newer = "2025-03-11")

library(tensorflow)
tf$version$VERSION
#> [1] "2.18.0"
tf$constant("Hello World")
#> tf.Tensor(b'Hello World', shape=(), dtype=string)

reticulate::py_config()
#> python:         D:/pkg_cache/uv/archive-v0/KJD_3H8jVXyWZwTpHqRAy/Scripts/python.exe
#> libpython:      C:/Users/margusl/AppData/Roaming/uv/python/cpython-3.11.11-windows-x86_64-none/python311.dll
#> pythonhome:     D:/pkg_cache/uv/archive-v0/KJD_3H8jVXyWZwTpHqRAy
#> virtualenv:     D:/pkg_cache/uv/archive-v0/KJD_3H8jVXyWZwTpHqRAy/Scripts/activate_this.py
#> version:        3.11.11 (main, Mar 17 2025, 21:01:29) [MSC v.1943 64 bit (AMD64)]
#> Architecture:   64bit
#> numpy:          D:/pkg_cache/uv/archive-v0/KJD_3H8jVXyWZwTpHqRAy/Lib/site-packages/numpy
#> numpy_version:  1.26.4
#> tensorflow:     D:\pkg_cache\uv\archive-v0\KJD_3H8jVXyWZwTpHqRAy\Lib\site-packages\tensorflow
#> 
#> NOTE: Python version was forced by py_require()
reticulate::py_list_packages()
#>                         package   version                          requirement
#> 1                       absl-py     2.1.0                       absl-py==2.1.0
#> 2                    astunparse     1.6.3                    astunparse==1.6.3
#> 3                       certifi 2025.1.31                   certifi==2025.1.31
#> 4            charset-normalizer     3.4.1            charset-normalizer==3.4.1
#> 5                   flatbuffers   25.2.10                 flatbuffers==25.2.10
#> 6                          gast     0.6.0                          gast==0.6.0
#> 7                  google-pasta     0.2.0                  google-pasta==0.2.0
#> 8                        grpcio    1.71.0                       grpcio==1.71.0
#> 9                          h5py    3.13.0                         h5py==3.13.0
#> 10                         idna      3.10                           idna==3.10
#> 11                        keras     3.9.0                         keras==3.9.0
#> 12                     libclang    18.1.1                     libclang==18.1.1
#> 13                     Markdown       3.7                        Markdown==3.7
#> 14               markdown-it-py     3.0.0                markdown-it-py==3.0.0
#> 15                   MarkupSafe     3.0.2                    MarkupSafe==3.0.2
#> 16                        mdurl     0.1.2                         mdurl==0.1.2
#> 17                    ml-dtypes     0.4.1                     ml-dtypes==0.4.1
#> 18                        namex     0.0.8                         namex==0.0.8
#> 19                        numpy    1.26.4                        numpy==1.26.4
#> 20                   opt_einsum     3.4.0                    opt_einsum==3.4.0
#> 21                       optree    0.14.1                       optree==0.14.1
#> 22                    packaging      24.2                      packaging==24.2
#> 23                     protobuf    5.29.3                     protobuf==5.29.3
#> 24                     Pygments    2.19.1                     Pygments==2.19.1
#> 25                     requests    2.32.3                     requests==2.32.3
#> 26                         rich    13.9.4                         rich==13.9.4
#> 27                          six    1.17.0                          six==1.17.0
#> 28                  tensorboard    2.18.0                  tensorboard==2.18.0
#> 29      tensorboard-data-server     0.7.2       tensorboard-data-server==0.7.2
#> 30                   tensorflow    2.18.0                   tensorflow==2.18.0
#> 31 tensorflow-io-gcs-filesystem    0.31.0 tensorflow-io-gcs-filesystem==0.31.0
#> 32             tensorflow_intel    2.18.0             tensorflow_intel==2.18.0
#> 33                    termcolor     2.5.0                     termcolor==2.5.0
#> 34            typing_extensions    4.12.2            typing_extensions==4.12.2
#> 35                      urllib3     2.3.0                       urllib3==2.3.0
#> 36                     Werkzeug     3.1.3                      Werkzeug==3.1.3
#> 37                        wrapt    1.17.2                        wrapt==1.17.2

Thanks for a very helpful reply. I still have some doubts:

  1. The dictionary meaning of ephemeral is, "lasting for a very short time". What does this mean in the present context? Will a new, compatible version of Python and required packages be installed for the current session only?
  2. Will a compatible version of Python be installed via the commands in Rstudio:
    Sys.setenv("RETICULATE_PYTHON" = "managed") reticulate::py_require(exclude_newer = "2025-03-11")
  3. I have the Python version 3.13.7 installed via Anaconda. I have the path to this included in the "Environment variables" in my windows. I also have a reference to this in the Global options for python in Rstudio settings. Shall I leave all this untouched, or should I make any changes here?
  4. Shall I use similar commands for installing keras (as for tensorflow above)?
  5. Are there any other points that I should think of?
    Once again, thanks a lot. You are an angel!

Just one more point. Somewhere in the Rstudio documentation (it can have been elsewhere), I had noted a reference to something referred to as the
"The Iron Law of Python Management".

  • Create a virtual environment for every project.
    Does the ephemeral procedure mean that this "Iron Law" is no longer valid? It would be nice if you can elaborate, if you see a reason for doing it.
  1. yes and no. Requirements are declared per session but environments are cached -- if there already is a matching cached environment, it will be used; If not, it will be created.
  2. Python version is part of dependencies and by default uv takes care of it, you can also set Python version constraints, if required, e.g. py_require(python_version = "3.12"). Most requirements here are actually declared behind the scenes when calling library(tensorflow):
Sys.setenv("RETICULATE_PYTHON" = "managed")
reticulate::py_require(exclude_newer = "2025-03-11")

library(tensorflow)
# list currently declared requirements and R packages that set those:
reticulate::py_require()
#> โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ• Python requirements โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•โ•
#> โ”€โ”€ Current requirements โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
#>  Python:   [No Python version specified. Will default to '3.11.11']
#>  Packages: numpy, tensorflow, numpy<2
#>  Exclude:  Anything newer than 2025-03-11
#> โ”€โ”€ R package requests โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
#> R package  Python packages                           Python version      
#> reticulate numpy                                                         
#> tensorflow tensorflow, numpy<2
  1. Pretty much everything related to (virtual) environment setup is highly opinionated and I'd rather not comment on anything directly related to Anaconda (the distribution). Though having a "global" conda environment goes strictly against that Iron Law . As long as you have RETICULATE_PYTHON" = "managed" env. variable set , reticulate should ignore all other hints (including Python in PATH or Python path set though RStudio settings), so probably do not need to change those settings for reticulate's sake.

  2. install.packages("keras3") (CRAN release is already using py_require(), keras package is old keras v2 for TensorFlow v2.15 or older)

Note that your question started as "Installing python tensorflow with Rstudio" , thanks to reticulate::py_require()is now more user-friendly than ever, in my opinion. But it's not quite clear what you meant by "work with python code". If you plan to use RStudio as an IDE for Python (as in writing & executing .py scripts, not just small inline bits in R or Quarto / RMarkdown chunks), py_require() approach might get bit clunky, especially when mixing with some other tooling like renv. Though you absolutely could just set something like this in your project's .Rprofile to configure an environment for Python scripts you plan to execute though R / RStudio:

Sys.setenv("RETICULATE_PYTHON" = "managed")
reticulate::py_require(python_version = "3.12")
reticulate::py_require("pandas)

There's no reason to have 2nd thoughts about having an environment per project, on the contrary, uv is actually pushing this further and makes it more convenient, e.g. by allowing to declare & embed requirements for stand-alone scripts. So perhaps it's worth going through uv intro & guides first, get some hand-on experience without mixing it with reticulate / R / RStudio just yet and then check Reticulate 1.41: Simplifying Python Installation and Package Management - Posit & linked Vignettes.

Thanks. I appreciate the help.

I have tried to follow your suggestions. Have found some problems:

> Sys.setenv("RETICULATE_PYTHON" = "managed")
> reticulate::py_require(exclude_newer = "2025-03-11")
> library(tensorflow)
> tf$version$VERSION
Error: Installation of Python not found, Python bindings not loaded.
See the Python "Order of Discovery" here: [https://rstudio.github.io/reticulate/articles/versions.html#order-of-discovery](https://rstudio.github.io/reticulate/articles/versions.html#order-of-discovery).
> #> [1] "2.18.0"
> tf$constant("Hello World")
Error in system2(uv, c("python list", "--all-versions", "--color never",  : 
  '"C:\Users\john\AppData\Local\R\cache\R\reticulate\uv\bin\uv.exe"' not found
Error: Installation of Python not found, Python bindings not loaded.
See the Python "Order of Discovery" here: [https://rstudio.github.io/reticulate/articles/versions.html#order-of-discovery](https://rstudio.github.io/reticulate/articles/versions.html#order-of-discovery).

I have python 3.12.7 version installed via Anaconda. And this is there both in the windows path environmental variable and in Rstudio Global options for python.

> Sys.setenv("RETICULATE_PYTHON" = "managed")
> reticulate::py_require(exclude_newer = "2025-03-11")

It appears that the above commands alone are not sufficient to install a compatible version of python. What is the method for addressing the message, "Python bindings not loaded"? How do I take care of the "order of discovery" problem? Should I install the python version 3.10 and run a use_python command with a path to that python version?
I thought that the reticulate_python "managed" command took care of all this automatically. If it does not, what should I do? The documentation on "order of discovery" is not entirely clear. I would appreciate some practical tips here.
Sorry for the late feedback on this.

I get the following:

> py_discover_config()
NULL

This tells me that I have to install a python additional to the 3.12.7 that I have. This means that I have to run the python_install() command. I would like to know the arguments for this. How do I know which is the compatible version? Are the any recommendations on where I should have this python version installed? Can I do it using a virtual environment in Conda?
Similar questions arise for other packages that are required? I can use the py_require() command. But how how do I know which versions are compatible? Can I keep these also in the virtual virtual environment. How much of the uv method should be kept free of conda and its virtual environments?
All of these can seem to be stupid questions. This is because I am a beginner to all this.

You just need to add the above line to your other reticulate commands. That will take care of installing Python for you. Anaconda and uv virtual environments are two different things and the virtual environment does not know about the Anaconda environment. You can't really use a mix of Anaconda and UV environments they are really designed to manage Python and Python packages in different ways.

states that reticulate attempted to use uv , but for some reason uv executable is not available.

You could install uv yourself (e.g. through WinGet) , but reticulate should be able fetch and set up its own instance if it can't find working one from your system. Or at least throw an error at earlier stage, so this might be a bug. Perhaps consider reporting it - https://github.com/rstudio/reticulate/issues .

You can find TensorFlow (the Python's pip package) requirements from https://www.tensorflow.org/install/pip#system_requirements :

Software requirements
  • Python 3.9โ€“3.12
  • pip version 19.0 or higher for Linux (requires manylinux2014 support) and Windows. pip version 20.3 or higher for macOS.

but it's not something you really need to worry about once you have sorted your reticulate & uv issues-- uv will resolve & handle both package and Python version dependencies, or in other words -- you should not do anything extra to get a compatible Python version. It just might not result with the latest possible Python version, e.g. you can see from previous sample session (Installing python tensorflow with Rstudio - #4 by margusl) that it settled with Python 3.11.11.

Declaring py_require(python_version = "3.12") attempts to use 3.12 instead, but if it happens to introduce a version conflict and dependencies can't be resolved, it will let you know.

I'm not really sure if py_discover_config() (not to be confused with py_config()) is really supposed to work with ephemeral environments in the same way as it handles conda & venv environments, i.e. it also returns NULL if there are no issues with reticulate and uv and py_config() initializes fully functional uv-backed environment. According to https://github.com/rstudio/reticulate/issues/1784 it seems to be expected, though maybe I'm placing that response into wrong context.

In your case the NULL -response is kind of expected as you've forced uv use though RETICULATE_PYTHON = "managed" but reticulate can't run uv.

Thanks, once again for being very helpful. I tried to install uv through winget, as you suggested. I did not succeed. This time, I got an alert message by my virus prevention software that the powershell command for installing uv was blocked. I think that I may have received this error message even earlier with the commands from Rstudio. This time, I disabled temporarily the block by the anti-virus software and ran the powershell command again. This time, everything ran perfectly.
cpython and 39 other packages were installed. Got the following results:
tf$version$VERSION

[1] "2.18.0"

> tf$constant("Hello World")
tf.Tensor(b'Hello World', shape=(), dtype=string)

Got some warning messages also about floating-point round-off errors also. Will worry about them later.

I will now try to get started and then come back if I run into problems.
Thanks a lot.

1 Like