[R-pkg-devel] Error in creating virtual environment on Debian machines

Taeyong Park t@eyongp @end|ng |rom @ndrew@cmu@edu
Thu Sep 5 22:40:00 CEST 2024


Hello,

I am trying to create a virtual environment in the zzz.r file of my
package, and my package is currently passing on Windows and failing on
Debian with 1 ERROR.


This is the ERROR:

* installing *source* package ‘PytrendsLongitudinalR’ ...
** using staged installation
** R
** inst
** byte-compile and prepare package for lazy loading
** help
*** installing help indices
** building package indices
** installing vignettes
** testing if installed package can be loaded from temporary location
Using Python: /usr/bin/python3.12
Creating virtual environment '/home/hornik/.virtualenvs/pytrends-in-r-new' ...
+ /usr/bin/python3.12 -m venv /home/hornik/.virtualenvs/pytrends-in-r-new
The virtual environment was not created successfully because ensurepip is not
available.  On Debian/Ubuntu systems, you need to install the python3-venv
package using the following command.

    apt install python3.12-venv

You may need to use sudo with that command.  After installing the python3-venv
package, recreate your virtual environment.

Failing command: /home/hornik/.virtualenvs/pytrends-in-r-new/bin/python3.12

FAILED
Error: package or namespace load failed for ‘PytrendsLongitudinalR’:
 .onLoad failed in loadNamespace() for 'PytrendsLongitudinalR', details:
  call: NULL
  error: Error creating virtual environment
'/home/hornik/.virtualenvs/pytrends-in-r-new' [error code 1]
Error: loading failed
Execution halted
ERROR: loading failed
* removing ‘/srv/hornik/tmp/CRAN_pretest/PytrendsLongitudinalR.Rcheck/PytrendsLongitudinalR’

and this is the onLoad function of my zzz.r file:


.onLoad <- function(libname, pkgname) {


  # Define paths
  venv_path <- file.path(Sys.getenv("HOME"), ".virtualenvs",
"pytrends-in-r-new")
  python_path <- file.path(venv_path, "bin", "python")

  # Ensure 'virtualenv' is installed
  if (!reticulate::py_module_available("virtualenv")) {
    reticulate::py_install("virtualenv")
  }


  # Check if the virtual environment exists
  if (!reticulate::virtualenv_exists(venv_path)) {
    # If it doesn't exist, attempt to create it
    tryCatch({
      reticulate::virtualenv_create(envname = venv_path, python =
python_path, force = TRUE, module =
getOption("reticulate.virtualenv.module"))
    }, error = function(e) {
      # Fallback: install Python and create the virtual environment
      reticulate::virtualenv_create(envname = venv_path, force = TRUE,
module = getOption("reticulate.virtualenv.module"))
    })
  }

  # Use the virtual environment for reticulate operations
  reticulate::use_virtualenv(venv_path, required = TRUE)

  # Install packages if not already installed
  packages_to_install <- c("pandas", "requests", "pytrends", "rich")
  for (package in packages_to_install) {
    if (!reticulate::py_module_available(package)) {
      reticulate::py_install(package, envname = "pytrends-in-r-new")
    }
  }


  TrendReq <<- reticulate::import("pytrends.request", delay_load =
TRUE)$TrendReq
  ResponseError <<- reticulate::import("pytrends.exceptions",
delay_load = TRUE)$ResponseError
  pd <<- reticulate::import("pandas", delay_load = TRUE)
  os <<- reticulate::import("os", delay_load = TRUE)
  glob <<- reticulate::import("glob", delay_load = TRUE)
  json <<- reticulate::import("json", delay_load = TRUE)
  requests <<- reticulate::import("requests", delay_load = TRUE)
  dt <<- reticulate::import("datetime", delay_load = TRUE)
  relativedelta <<- reticulate::import("dateutil.relativedelta",
delay_load = TRUE)
  time <<- reticulate::import("time", delay_load = TRUE)
  logging <<- reticulate::import("logging", delay_load = TRUE)
  console <<- reticulate::import("rich.console", delay_load = TRUE)$Console
  RichHandler <<- reticulate::import("rich.logging", delay_load =
TRUE)$RichHandler
  math <<- reticulate::import("math", delay_load = TRUE)
  platform <<- reticulate::import("platform", delay_load = TRUE)

  # Configure logging
  configure_logging()
}

How do I ensure that the virtual environment is created properly on
Debian systems?  I tried the instructions in the error message but
still got the error.

Thank you in advance for your help!


Best,

Taeyong




*Taeyong Park, Ph.D.*
*Assistant Teaching Professor of Statistics*
*Director, Statistical Consulting Center*
*Carnegie Mellon University Qatar*

	[[alternative HTML version deleted]]



More information about the R-package-devel mailing list