Problem with accessing virtual environment Python from R Markdown

Viewed 463

Note: I'm on Windows using Git Bash.

So, I am trying to setup a dev environment for the work for my class. It is going to involve a combination of coding in R and Python.

I created virtual environments using pipenv and virtualenv and ran into the same problem with both. So, first, let's create a virtual environment for the project in a sub-folder dev_env:

 cd project_folder/dev_env
pipenv --python 3.7
pipenv --py

Output

C:\Users\Ra Me\.virtualenvs\dev_env-5TUtSZI9\Scripts\python.exe

Now I'm going into my file.rmd and trying the reticulate package.

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

Next, I tried 2 methods:

Sys.setenv(RETICULATE_PYTHON = "C:/Users/Ra Me/.virtualenvs/dev_env-5TUtSZI9/Scripts")

or

use_virtualenv("C:/Users/Ra Me/.virtualenvs/dev_env-5TUtSZI9/", required = TRUE)
x = 1
if x:
    print('Hello!')

Both of them produced the error

Fatal Python error: initfsencoding: unable to load the file system codec
ModuleNotFoundError: No module named 'encodings'

However, when I change the path to the Python environment that's installed for all users on my machine, it works.

Sys.setenv(RETICULATE_PYTHON = "C:/Program Files/Python37/")

This method also works. However, here we are not even using the reticulate project.

knitr::opts_chunk$set(engine.path = list(
  python = "C:/Program Files/Python37/python.exe",
  r = "C:/Program Files/R/R-3.6.1/bin/R.exe"
))
0 Answers
Related