R markdown with python/reticulate - 没有名为 pandas 的模块

问题描述 投票:0回答:1

在得知 pandas 已安装后,我立即收到 pandas 的 ModuleNotFoundError 错误。 这是显示问题的简化代码。

{r echo=FALSE, include=FALSE}
knitr::opts_chunk$set(engine.path=list(python='/mnt/c/Users/steve/workspace/EDGI/EEW-ReportCard-Data/venv/bin/python'))
library(reticulate)
virtualenv_create('venv')
py_install('pandas', envname='venv')

u <- import('pandas')

当我运行这个块时,我得到了这个令人费解的结果,首先说满足了 pandas 的要求,然后找不到模块。我错过了什么?

> knitr::opts_chunk$set(engine.path=list(python='/mnt/c/Users/steve/workspace/EDGI/EEW-ReportCard-Data/venv/bin/python'))
library(reticulate)
virtualenv_create('venv')
py_install('pandas', envname='venv')

u <- import('pandas')
> library(reticulate)
> virtualenv_create('venv')
virtualenv: venv
> py_install('pandas', envname='venv')
Using virtual environment 'venv' ...
+ /home/steve/.virtualenvs/venv/bin/python -m pip install --upgrade --no-user pandas
Requirement already satisfied: pandas in /home/steve/.virtualenvs/venv/lib/python3.8/site-packages (2.0.3)
Requirement already satisfied: python-dateutil>=2.8.2 in /home/steve/.virtualenvs/venv/lib/python3.8/site-packages (from pandas) (2.9.0.post0)
Requirement already satisfied: pytz>=2020.1 in /home/steve/.virtualenvs/venv/lib/python3.8/site-packages (from pandas) (2024.1)
Requirement already satisfied: tzdata>=2022.1 in /home/steve/.virtualenvs/venv/lib/python3.8/site-packages (from pandas) (2024.1)
Requirement already satisfied: numpy>=1.20.3 in /home/steve/.virtualenvs/venv/lib/python3.8/site-packages (from pandas) (1.24.4)
Requirement already satisfied: six>=1.5 in /home/steve/.virtualenvs/venv/lib/python3.8/site-packages (from python-dateutil>=2.8.2->pandas) (1.16.0)
> 
> u <- import('pandas')
Error in py_module_import(module, convert = convert) : 
  ModuleNotFoundError: No module named 'pandas'
Run `reticulate::py_last_error()` for details.

我在 wsl 上使用 R 版本 3.6.3、Python 版本 3.8.10、Ubuntu 22.04。

python r r-markdown reticulate
1个回答
0
投票

感谢@margusl!问题是我的 virtualenv_create() 和 py_install('pandas') 正在 ~/.virtualvenvs 中的新 venv 中制作和安装,但我的 import('pandas') 正在 ./venv 中工作,因为这就是设置的在 knitr::opts_chunk$set() 中。

{r echo=FALSE, include=FALSE}
knitr::opts_chunk$set(engine.path=list(python='/mnt/c/Users/steve/workspace/EDGI/EEW-ReportCard-Data/venv/bin/python'))
library(reticulate)
py_install('pandas', envname='/mnt/c/Users/steve/workspace/EDGI/EEW-ReportCard-Data/venv')

u <- import('pandas')
© www.soinside.com 2019 - 2024. All rights reserved.