Skip to content

Instantly share code, notes, and snippets.

@gederajeg
Created July 4, 2024 06:01
Show Gist options
  • Save gederajeg/c8e460db1272e3ffd2fa76be6f5c2caa to your computer and use it in GitHub Desktop.
Save gederajeg/c8e460db1272e3ffd2fa76be6f5c2caa to your computer and use it in GitHub Desktop.
Setting up Python in RStudio on Windows

How to run properly python in R with reticulate (in Windows)

  • RStudio doesn't like the Microsoft app installation of python

  • So, install python via Anaconda

  • install the reticulate package

  • Find out the executable python in the system by opening the iPython interpreter from Anaconda, then run the code below:

    • import os

    • import sys

    • os.path.dirname(sys.executable) (this line will produce something like: "C:\Users\GRajeg\anaconda3", which in R/mac needs to be changed into "C:/Users/GRajeg/anaconda3")

  • in RStudio, given the available path of python exe from the previous step, run the following: reticulate::use_python("C:/Users/GRajeg/anaconda3")

    • If successful, no output is produced
  • Next, provided the previous step is successful, we can list python file in the anaconda3 domain:

list.files("C:/Users/GRajeg/anaconda3", pattern = "python")
# [1] "python.exe"    "python.pdb"    "python3.dll"   "python312.dll" "python312.pdb" "pythonw.exe"   "pythonw.pdb"
  • Test the python by:

    • Creating a new python script

    • type in: print("Hello World")

    • run that python code (similar like executing regular R script); it should produce something like this in the console:

> reticulate::repl_python()
Python 3.12.4 (C:/Users/GRajeg/anaconda3/python.exe)
Reticulate 1.38.0 REPL -- A Python interpreter in R.
Enter 'exit' or 'quit' to exit the REPL and return to R.
>>> print("Hello World")
Hello World
>>> 
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment