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