Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

To run Python in RStudio, install Python and the R reticulate package, select the intended Python environment before any Python code starts, and then use reticulate to import modules, run scripts, or open a Python console. The key diagnostic command is py_config(), which shows the interpreter RStudio is actually using.

1. Install Python and reticulate

Python must be installed separately from RStudio. If you want reticulate to manage a local Python distribution, Posit documents reticulate::install_miniconda() as a recommended route.

install.packages("reticulate")
library(reticulate)

Run these commands in the RStudio Console. Loading reticulate makes Python interoperability available in the current R session.

2. Choose the Python environment before using Python

Reticulate initializes Python lazily. Select the interpreter or environment before calling import(), py_run_file(), repl_python(), or another Python-dependent function.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use a specific Python executable

library(reticulate)
use_python("/path/to/python", required = TRUE)

Use a virtual environment

use_virtualenv("myenv", required = TRUE)

Use a Conda environment

use_condaenv("myenv", required = TRUE)

These selectors apply to the active R session. If you change the interpreter after Python has already started, restart the R session and make the selection again before importing anything.

With reticulate 1.41 and later, manually selecting an interpreter is often unnecessary when you declare requirements with py_require(); reticulate can resolve an ephemeral environment automatically. For projects that already depend on a known interpreter, explicit selection remains the clearest option.

3. Confirm what RStudio is using

py_config()

Check the output before troubleshooting. It identifies the Python executable, version, and environment visible to the current R session. A terminal may use a different Python installation, so a package that imports successfully there can still be unavailable in RStudio.

4. Install packages into that same environment

After selecting the environment, install Python packages through reticulate:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
py_install(c("numpy", "pandas"), envname = "myenv")

py_install() installs into a virtualenv or Conda environment. If envname is omitted, reticulate uses the environment named by RETICULATE_PYTHON_ENV; when that variable is unset, it uses the r-reticulate environment. Select the intended environment explicitly when the package exists in more than one place.

5. Four ways to run Python from RStudio

Method Use it for Key function
Import a module Calling Python libraries and functions from R import()
Source a script Loading functions and objects into the R session source_python()
Run a file Executing a Python file with controlled conversion py_run_file()
Interactive REPL Exploration and quick Python commands repl_python()

Import a Python module

library(reticulate)
np <- import("numpy")
np$array(c(1, 2, 3))

The imported object exposes Python modules, classes, and functions to R. Reticulate converts many common Python objects automatically. Use py_to_r() when you need explicit conversion.

Source a Python script

source_python("analysis.py")
result <- calculate_result(data)

Functions and objects defined in analysis.py become available in the R session.

Run a Python file

py_run_file("analysis.py", local = FALSE, convert = TRUE)

Here, convert = TRUE requests automatic conversion of returned Python objects. You can instead convert objects explicitly with py_to_r().

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Open an interactive Python console

repl_python()

Objects created in the embedded Python REPL remain available through reticulate’s shared Python state. Exit the REPL using its normal exit command or the RStudio interrupt controls.

6. Mix R and Python in R Markdown

Reticulate provides a Python language engine for R Markdown. An R Markdown document can contain both R and Python chunks, with shared objects and state, so you can use R-specific analysis alongside Python-only libraries in one reproducible report or notebook.

7. Fix the common environment and path problems

Python works in a terminal but not in RStudio

  1. Run py_config() in the RStudio Console and note the executable and environment.
  2. If it is not the intended interpreter, restart the R session.
  3. Call use_python(), use_virtualenv(), or use_condaenv() before importing any module.
  4. Install the missing package into that selected environment with py_install().
  5. Test the import again from RStudio, not only from a separate terminal.

A script cannot be found

Check RStudio’s working directory and the script name. Use an absolute path when the project directory may vary:

py_run_file("/absolute/path/to/analysis.py", local = FALSE, convert = TRUE)

Changing environments appears to have no effect

Python may already be initialized. Restart the R session, select the environment first, and then run py_config() followed by the import or script call.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

R and Python objects behave differently

Allow reticulate’s automatic conversion for common types, or make the boundary explicit with py_to_r() when you need predictable R objects.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

8. A reliable project workflow

  1. Install Python and reticulate.
  2. Open the project in RStudio and select its Python executable, virtualenv, or Conda environment.
  3. Restart the R session whenever you change that selection.
  4. Run py_config() to verify the active interpreter.
  5. Install dependencies with py_install() in the verified environment.
  6. Import modules, source files, run scripts, or use an R Markdown Python chunk.

The current Posit reference identifies reticulate 1.47.0 for py_install(). Because environment-resolution behavior and helper APIs can change, check the current Posit documentation when writing version-specific setup instructions.

Frequently Asked Questions

Do I need to install Python if RStudio is already installed?

Yes. RStudio is an IDE, while reticulate connects your R session to a separate Python installation or managed environment.

Why does reticulate say a package is missing when it works in my terminal?

The terminal and RStudio are likely using different Python environments. Run py_config(), select the intended environment before importing, and install the package there with py_install().

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Can R and Python share data in one R Markdown document?

Yes. Reticulate’s Python engine lets R and Python chunks communicate through shared objects and state.

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.