Can't figure out how to use conda environment after reticulate::use_condaenv(path)
Asked Answered
R

3

11

I created a conda environment using the terminal:

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

I also created a trivial python script

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

print("Done")

In an R notebook, I now want to run that script from within the conda environment I created earlier.

I tried:

reticulate::use_condaenv("pathfinder_example_proj_env", required = TRUE)
reticulate::source_python("../python/python_model.py")

But I get the following error:

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

When I check the version of python reticulate is using I get:

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

I checked for available versions using 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

And as you can see, the virtual environment is listed. I'm just not sure how to use it.

I've read all the articles on the reticulate website:
https://rstudio.github.io/reticulate/index.html

I also found a couple of threads on Github:
https://github.com/rstudio/reticulate/issues/1
https://github.com/rstudio/reticulate/issues/292

Reputable answered 29/7, 2018 at 22:43 Comment(0)
S
11

This works for me;

library(reticulate)

myenvs=conda_list()

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

restart r session is needed sometimes.

Seismism answered 16/10, 2020 at 7:36 Comment(1)
Works for me! I was using python path, instead env name. Thanks!Alvarez
R
2

I found the solution here: https://community.rstudio.com/t/reticulate-source-python-and-exec-problems/7386/6

After installing the development version of reticulate (devtools::install_github("rstudio/reticulate") reticulate uses the conda environment as expected.

Leaving this post up in case anyone else runs into this issue.

Reputable answered 29/7, 2018 at 22:56 Comment(0)
C
1

This thing worked:

By setting the value of the RETICULATE_PYTHON environment variable to a Python binary. Note that if you set this environment variable, then the specified version of Python will always be used (i.e. this is prescriptive rather than advisory). To set the value of RETICULATE_PYTHON, insert Sys.setenv(RETICULATE_PYTHON = PATH) into your project’s .Rprofile, where PATH is your preferred Python binary.

Changeling answered 15/2, 2019 at 13:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.