最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Having trouble using Python in R via Reticulate, issue with libraries - Stack Overflow

programmeradmin2浏览0评论

I am having a hard time getting Python to work in R via Reticulate. I downloaded Anaconda, R, Rstudio, and Python to my system. Below are their paths:

Python: C:\Users\John\AppData\Local\Microsoft\WindowsApps

Anaconda: C:\Users\John\anaconda3

R: C:\Program Files\R\R-4.2.1

Rstudio: C:\ProgramData\Microsoft\Windows\Start Menu\Programs

But within R, if I do "Sys.which("python")", the following path is displayed:

"C:\\Users\\John\\DOCUME~1\\VIRTUA~1\\R-RETI~1\\Scripts\\python.exe"

Now, whenever I call upon reticulate in R, it works, but after giving the error: "NameError: name 'library' is not defined"

I can use Python in R, but I'm unable to import any of the libraries that I installed, including pandas, numpy, etc. I installed those in Anaconda (though I used the "base" path when installing, as I didn't understand the whole 'virtual environment' thing). Trying to import a library results in the following error:

File "
C:\Users\John\AppData\Local\R\win-library\4.2\reticulate\python\rpytools\loader.py
", line 122, in _find_and_load_hook
    return _run_hook(name, _hook)
  File "
C:\Users\John\AppData\Local\R\win-library\4.2\reticulate\python\rpytools\loader.py
", line 96, in _run_hook
    module = hook()
  File "
C:\Users\John\AppData\Local\R\win-library\4.2\reticulate\python\rpytools\loader.py
", line 120, in _hook
    return _find_and_load(name, import_)
ModuleNotFoundError: No module named 'pandas'

Does anyone know of a resolution? Thanks in advance.

I am having a hard time getting Python to work in R via Reticulate. I downloaded Anaconda, R, Rstudio, and Python to my system. Below are their paths:

Python: C:\Users\John\AppData\Local\Microsoft\WindowsApps

Anaconda: C:\Users\John\anaconda3

R: C:\Program Files\R\R-4.2.1

Rstudio: C:\ProgramData\Microsoft\Windows\Start Menu\Programs

But within R, if I do "Sys.which("python")", the following path is displayed:

"C:\\Users\\John\\DOCUME~1\\VIRTUA~1\\R-RETI~1\\Scripts\\python.exe"

Now, whenever I call upon reticulate in R, it works, but after giving the error: "NameError: name 'library' is not defined"

I can use Python in R, but I'm unable to import any of the libraries that I installed, including pandas, numpy, etc. I installed those in Anaconda (though I used the "base" path when installing, as I didn't understand the whole 'virtual environment' thing). Trying to import a library results in the following error:

File "
C:\Users\John\AppData\Local\R\win-library\4.2\reticulate\python\rpytools\loader.py
", line 122, in _find_and_load_hook
    return _run_hook(name, _hook)
  File "
C:\Users\John\AppData\Local\R\win-library\4.2\reticulate\python\rpytools\loader.py
", line 96, in _run_hook
    module = hook()
  File "
C:\Users\John\AppData\Local\R\win-library\4.2\reticulate\python\rpytools\loader.py
", line 120, in _hook
    return _find_and_load(name, import_)
ModuleNotFoundError: No module named 'pandas'

Does anyone know of a resolution? Thanks in advance.

Share Improve this question edited Jan 19 at 6:34 IRTFM 263k22 gold badges378 silver badges499 bronze badges asked Jan 19 at 2:41 LifeisGood94LifeisGood94 677 bronze badges 4
  • Working in an old version of R will make it more complicated for general use. Current version is 4.4.2 – IRTFM Commented Jan 19 at 6:36
  • Library directories are often version dependent. – IRTFM Commented Jan 19 at 8:31
  • thanks. I re-installed (again) with the most recent version. No change – LifeisGood94 Commented Jan 19 at 17:31
  • You need to post complete output of new error messages and the output of sessionInfo() – IRTFM Commented Jan 20 at 9:48
Add a comment  | 

2 Answers 2

Reset to default 1

From included details I'd say that you had a fully functional reticulate setup with its default virtual environment, you just installed your Python packages to somewhere else.

VIRTUA~1\R-RETI~1 is a r-reticulate env that reticulate created for you at some point, it's also a default / fall-back env that it uses unless hinted otherwise through environment variables or by calling use_virtualenv() / use_condaenv(). When reticulate initializes Python in current session, it also prepends env's python executable to PATH and that's why Sys.which("python") points to that location. And this is expected behaviour.

You just need to make sure this[r-reticulate env] is also where you install your required packages to. And you could also do it from R through reticulate:

library(reticulate)
# reticulate defaults to r-reticulate env
py_install("pandas")
py_list_packages()
import("pandas")$`__version__`

If your setup is not quite functional anymore, here's one recipe you could try that shouldn't mess it up much further :

  • install mamba / Miniforge through winget to C:\Users\yourusername\miniforge3\ instead of using full Anaconda distribution
  • manage environments & install packages through reticulate in R
  • set default conda executable & conda environment variables in .Renviron to provide hints for reticulate

Minforge comes with pre-configured conda-forge channel and mamba, a faster & leaner drop-in replacement of conda; package repositories / channels are still the same. Assuming more or less recent Windows version where winget is available, in Windows powershell or cmd prompt:

winget install --id=CondaForge.Miniforge3 -e

In R:

# force reticulate to use a particular conda binary, test if it works and
# create a new Python 3.12 environment with pandas and its dependenices
options(reticulate.conda_binary = "C:/Users/margusl/miniforge3/condabin/conda.bat")
reticulate::conda_list()
#>      name                                                                     python
#> 1    base                                  C:\\Users\\margusl\\miniforge3/python.exe

reticulate::conda_create(envname = "py312", python_version = "3.12", packages = c("pandas"))
#> + "C:/Users/margusl/miniforge3/condabin/conda.bat" create --yes --name py312 "python=3.12" pandas --quiet -c conda-forge
#> [1] "C:\\Users\\margusl\\miniforge3\\envs\\py312/python.exe"
reticulate::conda_list()
#>      name                                                                     python
#> 1    base                                  C:\\Users\\margusl\\miniforge3/python.exe
#> 2   py312                     C:\\Users\\margusl\\miniforge3\\envs\\py312/python.exe

Set RETICULATE_CONDA and RETICULATE_PYTHON_ENV in ~/.Renviron (can be opened with usethis::edit_r_environ()):

RETICULATE_CONDA=C:/Users/margusl/miniforge3/condabin/conda.bat
RETICULATE_PYTHON_ENV=py312

Restart R and test if reticulate activates correct environment:

library(reticulate)
py_config()
#> python:         C:/Users/margusl/miniforge3/envs/py312/python.exe
#> libpython:      C:/Users/margusl/miniforge3/envs/py312/python312.dll
#> pythonhome:     C:/Users/margusl/miniforge3/envs/py312
#> version:        3.12.8 | packaged by conda-forge | (main, Dec  5 2024, 14:06:27) [MSC v.1942 64 bit (AMD64)]
#> Architecture:   64bit
#> numpy:          C:/Users/margusl/miniforge3/envs/py312/Lib/site-packages/numpy
#> numpy_version:  2.2.1
#> 
#> NOTE: Python version was forced by RETICULATE_PYTHON_ENV
py_list_packages()[17:21,]
#>    package version   requirement     channel
#> 17   numpy   2.2.1   numpy=2.2.1 conda-forge
#> 18 openssl   3.4.0 openssl=3.4.0 conda-forge
#> 19  pandas   2.2.3  pandas=2.2.3 conda-forge
#> 20     pip  24.3.1    pip=24.3.1 conda-forge
#> 21  python  3.12.8 python=3.12.8 conda-forge

pd <- import("pandas")
pd$DataFrame(data = list(A = 1:3, B = 11:13))
#>   A  B
#> 1 1 11
#> 2 2 12
#> 3 3 13

Of course you can manage that same environment directly with conda / mamaba, Minforge installation adds Minforge Prompt to start menu.

It looks like R is not using the correct Python executable. You can check your config with:

py_config()

It should be the one from anaconda3. If it's not, you can change to:

library(reticulate)
use_python("C:/Users/John/anaconda3/python.exe", required = TRUE)

After this it should work, otherwise you can try to install pandas in anaconda's prompt.

conda install pandas numpy

After all of this, just try importing pandas:

py_run_string("import pandas as pd")

Documentation, for more details

发布评论

评论列表(0)

  1. 暂无评论