Python: Using RStudio with Conda, pyenv + pyenv-virtualenv or pyenv + Poetry ver. 6

Last Updated on October 22, 2022 by shibatau

Typos are corrected.

I. What shall I use?

Any time you’re working on a Python project, you should first create a virtual environment. I use the following combinations.

1.On my iMac (Big Sur)

Conda + RStudio

2. On my Kali Linux (Debian)

pyenv + pyenv-virtualenv + RStudio

or

pyenv + Poetry + RStudo

II. The difference among complicating tools

There are some packages and plugins. Here is a simple explanation:

pyenv: manages multiple versions of Python itself.

virtualenv / venv: manages virtual environments for a specific Python version.

pyenv-virtualenv: is a plugin for pyenv and mangas virtual environments for across varying versions of Python.

I have quoted the explanation above from the post:

Managing Multiple Python Versions With pyenv

You can learn more from the link below though they are really complicating.

What is the difference between venv, pyvenv, pyenv, virtualenv, virtualenvwrapper, pipenv, etc?

III. RStudio with Conda

1.Creating a project in R

File > New Project

2.Installing the Reticulate package

Run the code in the Console:

install.packages("reticulate")

3.Creating a virtual environment

You are in the directory of your project when you open the terminal in RStudio. Run the code in the terminal.

conda create --prefix ./envs python=3.8 numpy pandas 

4.Activating the virtual environment

Run the command in the terminal:

conda activate ./envs

You can check the path to the Python interpreter with the command in the terminal:

which python

5.Install libraries

You can add libraries with pip but you should upgrade pip first.

pip install --upgrade pip
pip install seaborn

6.Make RStudio know where to find the Python interpreter

Create a .Rprofile file with your preferred text editor. I usually use Nvim, so I’ve run the code in the terminal:

nvim .Rprofile

Type the following command and save the file:

Sys.setenv(RETICULATE_PYTHON="./envs/bin/python")

You should restart R:

Session > Restart R

7.Run the python codes

Open a new Python Script, write codes in the editor and run them:

8.Deactivate a virtual environment

Run the command in the terminal.

conda deactivate

9.Remove a virtual environment

Run the command in the terminal.

conda env remove -p /Users/... /yourEnv

10.Show all the virtual environments

Run the command in the terminal.

conda info --envs

You can learn more here:

How to Use Python in R with Reticulate and Conda

virtualenv – how to specify new environment location for conda create

IV. RStudio with Pyenv + Pyenv-virtualenv

1.Create a project in RStudio

File > New Project

2.Install Python with Pyenv

The version of Python must be compiled with shared library support for Reticulate in RStudio. So the –enable-shared flag should be added. Run the command in the terminal.

env PYTHON_CONFIGURE_OPTS="--enable-shared" pyenv install 3.9.1

3.Create a virtual environment with Pyenv-virtualenv

Run the command in the terminal.

pyenv virtualenv 3.9.1 playground_1

4.Activate the virtual environment

The virtual environment will be activated every time you move to the project directory with the following command. Run the command in the terminal:

pyenv local playground_1

5.Upgrade pip

Run the command in the terminal:

pip install --upgrade pip

6.Install libraries with Pip

Run the command in the terminal:

pip install numpy

7.Make RStudio know where the interpreter is

Open Tools > Project Options > Python > Python interpreter and chose the appropriate interpreter on the list in the Virtual Environments tab. In my environment the path is:

~/.pyenv/versions/3.9.1/envs/playground_1/bin/python3.9

You will be required to restart RStudio.

8.Type Python codes and run them

Open File > Python Script, type codes in the Editor and run them with the Run button or Cntrl + Enter. You can run codes line by line.

in the console

You can learn more here:

Using reticulate in RStudio with pyenv

About shibatau

I was born and grown up in Kyoto. I studied western philosophy at the University and specialized in analytic philosophy, especially Ludwig Wittgenstein at the postgraduate school. I'm interested in new technology, especially machine learning and have been learning R language for two years and began to learn Python last summer. Listening toParamore, Sia, Amazarashi and MIyuki Nakajima. Favorite movies I've recently seen: "FREEHELD". Favorite actors and actresses: Anthony Hopkins, Denzel Washington, Ellen Page, Meryl Streep, Mia Wasikowska and Robert DeNiro. Favorite books: Fyodor Mikhailovich Dostoyevsky, "The Karamazov Brothers", Shinran, "Lamentations of Divergences". Favorite phrase: Salvation by Faith. Twitter: @shibatau

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.