网状::: use_condaenv(path)后无法弄清楚如何使用conda环境

时间:2018-07-29 22:43:26

标签: r rstudio reticulate

我使用终端创建了一个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笔记本中,我现在想在我之前创建的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

当我检查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

您会看到,列出了虚拟环境。我只是不确定如何使用它。

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

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

3 个答案:

答案 0 :(得分:1)

我在这里找到了解决方法:https://community.rstudio.com/t/reticulate-source-python-and-exec-problems/7386/6

在安装了reticulate的开发版本(devtools :: install_github(“ rstudio / reticulate”)之后,reticulate将按预期使用conda环境。

如果其他人遇到此问题,则保留此帖子。

答案 1 :(得分:0)

这件事奏效了:

通过将RETICULATE_PYTHON环境变量的值设置为Python二进制文件。请注意,如果您设置此环境变量,则将始终使用指定版本的Python(即,这是说明性的,而非建议性的)。要设置RETICULATE_PYTHON的值,请将Sys.setenv(RETICULATE_PYTHON = PATH)插入您的项目的.Rprofile ,其中PATH是您首选的Python二进制文件。

答案 2 :(得分:0)

这对我有用;

library(reticulate)

myenvs=conda_list()

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

有时需要重新启动会话。

相关问题