rstudio / reticulate

R Interface to Python
https://rstudio.github.io/reticulate
Apache License 2.0
1.68k stars 328 forks source link

reticulate repl_python() failed on import while python interpreter succeeded #1690

Open fafesche opened 3 weeks ago

fafesche commented 3 weeks ago

Hi, I used reticulate to install python 3.12:latest and configure a virtual environment. In that environment, I have installed spyder whose startup script is just a python script.

the main command to launch spyder is the following: from spyder.app.start import main

I tested two scenarios: 1] Use reticulate::repl_python(): copy this command and get: _Python 3.12.7 (/home/fafesche/.myEnv/12_IAE-M1/bin/python) Reticulate 1.39.0 REPL -- A Python interpreter in R. Enter 'exit' or 'quit' to exit the REPL and return to R.

from spyder.app.start import main Traceback (most recent call last): File "", line 1, in File "/home/fafesche/R/x86_64-pc-linux-gnu-library/4.4/reticulate/python/rpytools/loader.py", line 122, in _find_and_load_hook return _run_hook(name, _hook) ^^^^^^^^^^^^^^^^^^^^^^ File "/home/fafesche/R/x86_64-pc-linux-gnu-library/4.4/reticulate/python/rpytools/loader.py", line 96, in _run_hook module = hook() ^^^^^^ File "/home/fafesche/R/x86_64-pc-linux-gnu-library/4.4/reticulate/python/rpytools/loader.py", line 120, in _hook return _find_andload(name, import) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home/fafesche/.myEnv/12_IAE-M1/lib/python3.12/site-packages/spyder/app/start.py", line 22, in import logging File "/home/fafesche/R/x86_64-pc-linux-gnu-library/4.4/reticulate/python/rpytools/loader.py", line 122, in _find_and_load_hook return _run_hook(name, _hook) ^^^^^^^^^^^^^^^^^^^^^^ File "/home/fafesche/R/x86_64-pc-linux-gnu-library/4.4/reticulate/python/rpytools/loader.py", line 96, in _run_hook module = hook() ^^^^^^ File "/home/fafesche/R/x86_64-pc-linux-gnu-library/4.4/reticulate/python/rpytools/loader.py", line 120, in _hook return _find_andload(name, import) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ModuleNotFoundError: No module named 'logging'_

2] Use python - from the environment - using source activate (under linux): same command no error.

This failed under linux (mint) and under Windows (v10). I get the same error in both cases.

I also tried to launch the spyder script using reticulate::py_run_file() got the same error.

My py_config() is the following: _> py_config() python: /home/fafesche/.myEnv/12_IAE-M1/bin/python libpython: /home/fafesche/.pyenv/versions/3.12.7/lib/libpython3.12.so pythonhome: /home/fafesche/.myEnv/12_IAE-M1:/home/fafesche/.myEnv/12_IAE-M1 version: 3.12.7 (main, Oct 21 2024, 16:36:21) [Clang 14.0.6 ] numpy: /home/fafesche/.myEnv/12_IAE-M1/lib/python3.12/site-packages/numpy numpyversion: 2.1.2

To configure my virtual env, I have used the environment variable WORK_HOME as documented to put the virtual environment into another directory than the default one.

The encountered error means that it is not possible to run spyder from the repl_python() of reticulate which could be cool when working with students :)

Thanks in advance, Fabien

t-kalinowski commented 3 weeks ago

Hi, could you provide instructions I can follow to reproduce the error locally?

fafesche commented 3 weeks ago

Hi, a minimal script which mimics what I did - perhaps incorrectly - is the following:

Sys.setenv(WORKON_HOME = "C:/IAEpython")
library(reticulate)
pver <- "3.12"
reticulate::install_python(version=pver)
myEnv.name <- "myEnv"
reticulate::virtualenv_create(myEnv.name, python = pver)
reticulate::use_virtualenv(myEnv.name)

fn.pkg_install <- function(name = "", my.env = "") {
  if (requireNamespace("reticulate", quietly = TRUE)) {
    if (!reticulate::py_module_available(name)) {
      reticulate::py_install(name, my.env)
      return(TRUE)
    }
  }
  return(FALSE)
}
pkgs <- c("pandas", "spyder")
res <- sapply(pkgs, fn.pkg_install, my.env = myEnv.name)
reticulate::repl_python()
# Enter from spyder.app.start import main
# -> ModuleNotFoundError: No module named 'logging' after a long trace

If I activate the python env using command-line then launch python - the one of the environment which is btw the only python installed in the system - i get no error from this import and thus spyder is perfectly working in that environment. Thanks in advance, Fabien