【问题标题】:Can't figure out how to use conda environment after reticulate::use_condaenv(path)在 reticulate::use_condaenv(path) 之后无法弄清楚如何使用 conda 环境
【发布时间】:2018-07-29 22:43:25
【问题描述】:

我使用终端创建了一个 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 正在使用的版本时,我得到:

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

【问题讨论】:

    标签: r rstudio reticulate


    【解决方案1】:

    这对我有用;

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

    有时需要重新启动会话。

    【讨论】:

    • 为我工作!我使用的是 python 路径,而不是环境名称。谢谢!
    【解决方案2】:

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

    安装reticulate的开发版后(devtools::install_github("rstudio/reticulate") reticulate按预期使用conda环境。

    留下这篇文章,以防其他人遇到这个问题。

    【讨论】:

      【解决方案3】:

      这件事奏效了:

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

      【讨论】:

        猜你喜欢
        • 2021-08-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-10-07
        • 2023-03-25
        • 2014-12-18
        • 1970-01-01
        相关资源
        最近更新 更多