Do these 3 things before closing this tab:
1Scan for outdated or missing drivers - takes under a minute2Repair Windows errors before they cause bigger problems3Fix the driver behind crashes, sound loss and screen glitchesUse RStudio’s reticulate package to embed Python in the active R session. Install Python and reticulate, select the intended interpreter before Python starts, verify it with py_config(), then import modules, run scripts, open a Python REPL, or combine Python and R in an R Markdown document.
Prerequisites and first setup
You need a working Python installation and the R package reticulate. In the RStudio Console, run:
install.packages("reticulate")
library(reticulate)
Posit also documents reticulate::install_miniconda() as a recommended route when you want reticulate to manage a local Miniconda distribution. Install Python before trying to import a module or execute a Python file.
Choose the Python environment before using Python
Reticulate initializes its Python bindings lazily. Make the environment selection your first Python-related operation in a new R session—before import(), py_run_file(), or similar calls.
#1 Best Overall
library(reticulate)
# Select a specific interpreter
use_python("/path/to/python", required = TRUE)
# Or select a virtualenv
use_virtualenv("myenv", required = TRUE)
# Or select a Conda environment
use_condaenv("myenv", required = TRUE)
Replace the path or environment name with the one used by your project. Selection applies to the current R session. If Python has already been initialized, restart the R session and select the interpreter again.
Automatic environment resolution with py_require()
With reticulate 1.41 and later, declaring requirements with py_require() can let reticulate resolve an ephemeral environment automatically, so manual selection is often unnecessary. Use explicit selectors when a project must run against a particular existing interpreter or environment.
Verify the interpreter RStudio is using
py_config()
Check the reported Python executable and environment before diagnosing missing packages, path errors, or differences between RStudio and a terminal.
Rank #2
Install Python packages into that same environment
Install dependencies through reticulate or the environment’s documented package manager, making sure the target environment is the one selected by the R session.
py_install(c("numpy", "pandas"), envname = "myenv")
py_install() installs into a virtualenv or Conda environment. If you omit envname, reticulate uses the environment selected by RETICULATE_PYTHON_ENV, or the r-reticulate environment when that variable is unset.
When a package exists in several environments, call use_virtualenv() or use_condaenv() first, restart if Python was already initialized, and then install or import the package. A successful terminal import does not prove that the RStudio session sees the same environment.
Four ways to run Python from RStudio
| Method | Use it when | Example |
|---|---|---|
import() |
You need to call functions, classes, or objects from a Python module. | np <- import("numpy") |
source_python() |
You want definitions from a Python script exposed directly in the R session. | source_python("analysis.py") |
py_run_file() |
You want to execute a Python file and control conversion and execution scope. | py_run_file("analysis.py", local = FALSE, convert = TRUE) |
repl_python() |
You want an interactive Python prompt embedded in the RStudio session. | repl_python() |
Import a module and call it
library(reticulate)
np <- import("numpy")
np$array(c(1, 2, 3))
import() exposes Python modules, classes, and functions to R. Reticulate converts many common Python objects to R automatically. For an explicit conversion, use py_to_r().
values <- np$array(c(1, 2, 3))
values_r <- py_to_r(values)
Load functions from a Python script
source_python("analysis.py")
result <- calculate_result(data)
Functions and objects defined in analysis.py become available in the R session after source_python() returns.
The Tool Desk
Outbyte PC Repair FREEClear out junk files and repair common Windows errorsFree Scan →Outbyte Driver Updater FREEFix the driver behind crashes, sound loss and screen glitchesFind Drivers →Execute a Python file
py_run_file("analysis.py", local = FALSE, convert = TRUE)
Set convert = TRUE when you want automatic conversion of returned objects. You can instead convert individual objects explicitly with py_to_r(). Use an absolute path or confirm the RStudio working directory if the file cannot be found.
Open an interactive Python REPL
repl_python()
Objects created in the embedded REPL remain in reticulate’s shared Python state and can be accessed from the R session through reticulate.
Use Python and R together in R Markdown
Reticulate provides a Python language engine for R Markdown. Python and R chunks can communicate through shared objects and state, letting one reproducible document use Python-specific libraries alongside R analysis.
Keep the environment choice reproducible: select or resolve the intended Python environment before the first Python chunk, and install its dependencies there. If a rendered document behaves differently from an interactive session, inspect py_config() in the session that performs the render.
Outdated Drivers Are Slowing You Down
One free scan finds every outdated or missing driver and matches the right update for your exact hardware.Free scan · exact hardware matchWindows Errors? Fix Them Before They Spread
Repair common Windows errors and clear accumulated junk for a smoother, more stable PC - no reinstall needed.Free scan · no reinstallBest Value
Fix the common “works in the terminal, not in RStudio” problem
- Inspect the active configuration. Run
py_config()in the RStudio Console and note the executable and environment. - Restart Python in the R session. Use RStudio’s session restart, then call
use_python(),use_virtualenv(), oruse_condaenv()before any import or file execution. - Install into the reported environment. Run
py_install()with the intendedenvname, or use that environment’s virtualenv/Conda installer. - Test the import inside RStudio. For example, run
import("numpy")in the same Console or document that will use it. - Check script paths. Confirm the working directory with R’s path tools or pass an absolute path to
source_python()orpy_run_file().
The key distinction is that RStudio embeds Python in the current R process; it does not automatically use whichever Python your separate terminal happens to invoke.
Which reticulate approach should you use?
- Call a library repeatedly: use
import()and keep the returned module object. - Reuse a Python utility script: use
source_python()when its functions should become ordinary R-session objects. - Run a complete file: use
py_run_file(), especially when you need explicit control over conversion or execution scope. - Explore interactively: use
repl_python(). - Publish a mixed-language report: use Python chunks in R Markdown and document the environment and dependencies.
Version note
Posit’s current py_install() reference identifies reticulate version 1.47.0. Environment resolution and helper APIs can change, so check the current Posit reticulate documentation when an instruction depends on a specific version.
Quick Recap
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




