无法弄清如何在使用reticulate::use_condaenv(path)后使用conda环境

10

我使用终端创建了一个 Conda 环境:

conda create --name pathfinder_example_proj_env python=3.6 feather-format=0.4.0 statsmodels=0.9.0

我还创建了一个简单的Python脚本。

import feather
import pandas as pd
import statsmodels.api as sm

print("Done")
在 R Notebook 中,我现在想从先前创建的 conda 环境中运行该脚本。 我尝试过:
reticulate::use_condaenv("pathfinder_example_proj_env", required = TRUE)
reticulate::source_python("../python/python_model.py")

但我遇到了以下错误:

Error in py_run_file_impl(file, local, convert) : ImportError: No module named feather

当我检查reticulate正在使用的Python版本时,得到以下结果:

reticulate::py_config()

python:         /usr/bin/python
libpython:      /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/config/libpython2.7.dylib
pythonhome:     /System/Library/Frameworks/Python.framework/Versions/2.7:/System/Library/Frameworks/Python.framework/Versions/2.7
version:        2.7.10 (default, Oct  6 2017, 22:29:07)  [GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.31)]
numpy:          /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/numpy
numpy_version:  1.8.0

python versions found: 
 /usr/bin/python
 /Users/bradcannell/anaconda/bin/python
 /Users/bradcannell/.virtualenvs/bradcannell-_MDC9FPE/bin/python

我使用py_discover_config()检查可用版本。

reticulate::py_discover_config()

python:         /usr/bin/python
libpython:      /System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/config/libpython2.7.dylib
pythonhome:     /System/Library/Frameworks/Python.framework/Versions/2.7:/System/Library/Frameworks/Python.framework/Versions/2.7
version:        2.7.10 (default, Oct  6 2017, 22:29:07)  [GCC 4.2.1 Compatible Apple LLVM 9.0.0 (clang-900.0.31)]
numpy:          /System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/numpy
numpy_version:  1.8.0

python versions found: 
 /usr/bin/python
 /Users/bradcannell/anaconda/bin/python
 /Users/bradcannell/.virtualenvs/bradcannell-_MDC9FPE/bin/python
 /Users/bradcannell/anaconda/envs/pathfinder_example_proj_env/bin/python

如您所见,虚拟环境已列出。我只是不确定如何使用它。

我已阅读了reticulate网站上的所有文章:
https://rstudio.github.io/reticulate/index.html

我还在Github上找到了几个线程:
https://github.com/rstudio/reticulate/issues/1
https://github.com/rstudio/reticulate/issues/292

3个回答

10

这对我有效;

library(reticulate)

myenvs=conda_list()

envname=myenvs$name[2]
use_condaenv(envname, required = TRUE)
# or
use_condaenv("r-miniconda", required = TRUE)

有时需要重新启动 R 会话。


可以用!我之前使用了Python路径,而不是环境名称。谢谢! - gonzalez.ivan90

2

1
这个东西有效:
通过将RETICULATE_PYTHON环境变量的值设置为Python二进制文件。请注意,如果您设置了此环境变量,则始终将使用指定版本的Python(即,这是规定性的而不是建议性的)。要设置RETICULATE_PYTHON的值,请将Sys.setenv(RETICULATE_PYTHON = PATH)插入到您的项目的.Rprofile中,在那里PATH是您首选的Python二进制文件。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接