Skip to content

TensorFlow + Python

What It Is

TensorFlow is our neural network computation engine. In R, we access TensorFlow through the keras3 and tensorflowpackages, but the actual math runs in Python under the hood.
We isolate this Python environment using reticulate::install_tensorflow() — it creates a virtual environment (r-tensorflow) so dependencies are managed cleanly and reproducibly.

Why It Matters

TensorFlow does the heavy lifting for deep learning. Without it, we can’t train neural networks. The reticulate bridge makes it possible to write in R while executing on Python-powered infrastructure.

✅ 1. Confirm Python Is Installed

We use Python 3.10+ for TensorFlow compatibility.
In R Console, confirm Python is available:
reticulate::py_config()
If no version is shown, install Python from , then restart R.

❌ If Python Is Not Installed:

Download Python 3.10 (not 3.11+ to avoid compatibility issues) here:
During installation:
✅ Check the box: “Add Python to PATH”
Choose Customize Installation and make sure pip is checked
Complete install
Restart R or RStudio.
Proceed to step 2

✅ 2. Install Virtual Environment + TensorFlow

Run this from a clean R session:
# Install TensorFlow and create the venv 'r-tensorflow'
tensorflow::install_tensorflow()
This will:
Create a virtual environment under: Documents/.virtualenvs/r-tensorflow
Install Python libraries: tensorflow, keras, numpy, etc.
Configure R to use this environment by default

✅ 3. Verify Install

After install completes, test with:
library(keras3)

model <- keras_model_sequential(input_shape = c(10)) |>
layer_dense(units = 32, activation = "relu") |>
layer_dense(units = 1)

summary(model)
You should see a printed model summary with layer shapes and parameters.


🧠 Common Pitfalls

Error: Python not found → Install Python manually, then rerun install_tensorflow()
CUDA/GPU errors → We use CPU only. Avoid GPU setup unless instructed
Permission issues → Always run R as Admin during setup

🐍 Python Troubleshooting Log

🔧 Issue #1: R Couldn’t Find Python
Error in py_config(): Python not found

Cause:
Python wasn’t installed or not added to the system PATH.
Fix:
Downloaded Python 3.10.11 from the official site:
✅ Made sure to check “Add Python to PATH” during installation
Restarted R/RStudio
Re-ran:
tensorflow::install_tensorflow()

🔧 Issue #2: Wrong Python Binary in PATH

Warning: cannot open file '.../python3.exe': The file cannot be accessed by the system
Cause:
R was detecting a phantom/misconfigured Python install in:
C:/Users/AaronContreras/AppData/Local/Microsoft/WindowsApps/python3.exe
This file was a stub (not a real Python binary).
Fix:
Manually installed the correct Python version (3.10)
Created a clean virtualenv in:
C:/Users/AaronContreras/Documents/.virtualenvs/r-tensorflow/
Verified correct Python path with:
library(reticulate)
py_config()
Expected output:
python: C:/Users/AaronContreras/Documents/.virtualenvs/r-tensorflow/Scripts/python.exe
version: 3.10.11

🔧 Issue #3: Libraries Failed to Install Due to Missing pip/wheel

Error: pip not found / setuptools not found
Cause:
Virtual environment was created, but pip, wheel, or setuptools were outdated or missing.
Fix:
tensorflow::install_tensorflow() automatically fixed it when rerun after Python was properly installed. It included:
+ python.exe -m pip install --upgrade pip wheel setuptools

🔧 Issue #4: Version Conflicts Between Keras and TensorFlow

Error in keras_model_sequential(): unexpected argument 'input_shape'
Cause:
Old keras package incompatible with new TensorFlow/keras3 backend.
Fix:
Installed and used the new keras3 package from CRAN:
install.packages("keras3")
library(keras3)
✅ Best Practice Going Forward
Step
Code
Check active Python version
py_config()
Reinstall virtualenv safely
tensorflow::install_tensorflow()
Load latest Keras package
library(keras3)
There are no rows in this table

Load content from share.geckoboard.com?
Loading external content may reveal information to 3rd parties. Learn more
Allow
Load content from share.geckoboard.com?
Loading external content may reveal information to 3rd parties. Learn more
Allow
Want to print your doc?
This is not the way.
Try clicking the ··· in the right corner or using a keyboard shortcut (
CtrlP
) instead.