Last Updated on March 27, 2023 by mishou

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 Scriptwrite codes in the editor and run them:

rstudio
rstudio

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

rstudio

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

I. You are supposed to be . . . like me
These posts are the tutorials just for people who want to be self taught in learning Python. By “self taught” I suppose you are not form a STEM field and won’t take any online courses because you want to learn what you love to learn.

No STEM background
No programming courses
No books on programming
Just have some knowledge of English
II. Zen of Lectures
You can learn better when you learn without any coercion.
You can learn deeper when you love it.
You want to learn more and more when you learn in a self taught way.
III. Mindset
Can you master programmings in an ordinary way, that is, as in school? Yes? Good, you should learn it in that way. No? Me neither. Then, let’s seek another way of learning. You can start anywhere you like, then, you will love it and be much better learners. This is my image of Autodidacticism:

https://colab.research.google.com/drive/1awrqQlR28bYHj_o4HuG07vCGr5inOkoU?usp=sharing
You are entirely free to start to learn anywhere in any way but you have to have a courage to suspend the questions that you cannot solve immediately, expecting they will be solved later.

You will love what you are learning in Autodidacticism. Let’s enjoy programming.

rstudio
rstudio

You can learn more here:

Using reticulate in RStudio with pyenv

By mishou

Leave a Reply

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