4

Getting the error message

ERROR: The requested version of Python
('~/.virtualenvs/python_environment/bin/python') cannot be used, as
another version of Python ('/usr/bin/python3') has already been
initialized. Please restart the R session if you need to attach
reticulate to a different version of Python.
Error in value[[3L]](cond) : 
  failed to initialize requested version of Python
Calls: local ... tryCatch -> tryCatchList -> tryCatchOne -> <Anonymous>
In addition: Warning message:
In py_initialize(config$python, config$libpython, config$pythonhome,  :
  '.Random.seed[1]' is not a valid integer, so ignored
Execution halted

When loading my web app on shiny apps. The app only loads after I refresh the web page

Here is part of my code:

library(shinyWidgets)
library(tidyverse)
library(reticulate)
library(DT)
library(data.table)
virtualenv_create(envname = "python_environment",python="python3")
virtualenv_install("python_environment", packages =c('pandas','catboost'))
use_virtualenv("python_environment",required = TRUE)
Emm
  • 2,367
  • 3
  • 24
  • 50
  • You could try setting a default python version in your `.Renviron` file. This might help: https://stackoverflow.com/a/51547674/5269252 – meenaparam Jan 30 '20 at 13:19

2 Answers2

5

When you run library(reticulate), the reticulate package will try to initialize a version of Python, which may not be the version that you intend to use. To avoid this, (in a new R session) run your set-up commands without importing the full reticulate library with the :: syntax like this:

reticulate::virtualenv_create(envname = 'python_environment', 
                              python= 'python3')
reticulate::virtualenv_install("python_environment", 
                               packages=c('pandas','catboost'))
reticulate::use_virtualenv("python_environment",required = TRUE)
Rani Powers
  • 196
  • 3
  • 6
0

To install package tensorflow (for example) to a certain Python environment:

install.packages("tensorflow")

reticulate::virtualenv_create(envname = "Experiment", python = "C:/Users/Acer/AppData/Local/Programs/Python/Python310/python.exe")
## Virtual environment 'Experiment' successfully created.

reticulate::virtualenv_install("Experiment", packages = "tensorflow")
## OK

reticulate::use_virtualenv("Experiment", required = TRUE)

library(tensorflow)
tf$constant("Hello Tensorflow!")
Mikael Jagan
  • 9,012
  • 2
  • 17
  • 48