'How to get reticulate to access virtual environment
I have a pre-existing virtual environment set up for my python requirements, D:/SC/venv. I am trying to import data from a pickle into RStudio. I gather that in order to do so, I need to run a python script to read the pickle in RStudio using reticulate. However, when I run:
use_virtualenv("D:/SourceControl/sportsenv")
path_to_python <- "D:/SourceControl/sportsenv"
use_python(path_to_python)
source_python("pickle_reader.py")
I get the following error message:
Error in py_run_file_impl(file, local, convert) :
ModuleNotFoundError: No module named 'pandas'
Pandas is installed and can be used with python directly. I think this is a problem with setting the virtual environment, as when I run py_config() I get:
> py_config()
python: C:/Program Files/Python37/python.exe
libpython: C:/Program Files/Python37/python37.dll
pythonhome: C:/Program Files/Python37
version: 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 22:22:05) [MSC v.1916 64 bit (AMD64)]
Architecture: 64bit
numpy: C:/Users/.../AppData/Roaming/Python/Python37/site-packages/numpy
numpy_version: 1.17.4
So I guess it hasn't recognised my virtual environment.
Solution 1:[1]
Just ran across this same issue and think I found a workaround. Although, on Windows, reticulate defaults to the global Python executable, you can force it to point to the Python executable within a virtual environment. The following example launches a Python REPL within a virtual environment called foo
at ~/.venv/foo/
that has pandas installed.
# Force reticulate to look for virtual env. Python executable
Sys.setenv("RETICULATE_PYTHON" = "~/.venv/foo/Scripts/python.exe")
# Launch REPL per usual
reticulate::repl_python()
#> Python 3.9.7 (C:/Users/DanielMolitor/Documents/.venv/foo/Scripts/python.exe)
#> Reticulate 1.22 REPL -- A Python interpreter in R.
#> Enter 'exit' or 'quit' to exit the REPL and return to R.
>>> import pandas
>>> pandas.DataFrame({'a': [1, 2, 3], 'b': [2, 3, 4]})
>>> a b
>>> 0 1 2
>>> 1 2 3
>>> 2 3 4
Solution 2:[2]
There is a function for this:
> use_virtualenv('./.venv', required=TRUE)
> py_config()
python: MYHOME/.venv/bin/python
libpython: /usr/local/opt/[email protected]/Frameworks/Python.framework/Versions/3.9/lib/python3.9/config-3.9-darwin/libpython3.9.dylib
pythonhome: MYHOME/.venv:MYHOME/.venv
virtualenv: MYHOME/.venv/bin/activate_this.py
version: 3.9.9 (main, Jan 6 2022, 22:03:21) [Clang 10.0.1 (clang-1001.0.46.4)]
numpy: MYHOME/.venv/lib/python3.9/site-packages/numpy
numpy_version: 1.22.3
NOTE: Python version was forced by use_python function
Note to get this to work for me, I had to re-install reticulate
. Presumably some old version had a bug. My current (working) version is 1.24.
> packageVersion('reticulate')
[1] ‘1.24’
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | |
Solution 2 | dfrankow |