usethis::edit_r_environ(scope = "project")
Suppose you were trying to run a quarto document in Rstudio. In that case, it will run fine if your default Python version has the correct libraries installed. But what if you are working on a project where project-specific libraries are to be installed and used? And all to be set up in an Anaconda Python environment in RStudio?
It is possible. However, setting up a custom Anaconda environment and using that specific Python version is not apparent. You might have to spend some time to figure out from Stackoverflow and some GitHub issue queues.
I’ve figured it out after spending a couple of hours. I wanted to document this in case I need it later, or someone else might need it.
Here it is.
Steps at a glance
- Create a Python environment using Anaconda distribution. I use this because it is easy to use
- Install the necessary libraries in that environment
- Create a Quarto project in RStudio
- Set the project-specific
Renviron
- Set the Python interpreter for your project in Rstudio
- Check if Quarto and Jupyter are properly connected to the Python version you want
- Render
Setting up Anaconda
I will skip this section, assuming that you either have Anaconda installed or are managing your Python environment in other ways. If you need help with that, please search on YouTube; there are good videos on this topic.
Setting up RStudio
The first is to create a separate Rstudio project to house your code and documents. We’re using Python with the Jupyter engine to run the Quarto document.
Check your current Jupyter engine
From within Rstudio, use the Terminal tab (bottom left panel). It should open the terminal within Rstudio. On the CLI, type the following.
quarto check jupyter
This should produce an output like the following:
[✓] Checking Python 3 installation....OK
Version: 3.8.13 (Conda)
Path: /Users/enayetraheem/opt/anaconda3/envs/ibis3813/bin/python
Jupyter: 4.10.0
Kernels: python3
[✓] Checking Jupyter engine render....OK
Notice that it shows the current Python engine, which is /Users/enayetraheem/opt/anaconda3/envs/ibis3813
. Also, we can see the Jupyter version here.
Where does Rstudio get this information from?
The short answer is: from the global .Renviron
file. In the current Renviron, this is what I have.
QUARTO_PYTHON=/Users/enayetraheem/opt/anaconda3/envs/ibis3813/bin/python
If you want to use this Python environment, then you are good. It should run Python without issues. Also, ensure new libraries are installed in this environment for you to use in this notebook.
The problem
The problem occurs when you want to use a different environment! The one you see above is the global environment. RStudio will choose this unless you specify a project-specific .Renviron
.
The solution seems easy.
Let’s take a look at the current setup.
There is no Python interpreter set for the current project. We can select one, but that won’t work.
Let’s set the Python interpreter to be the following:
Let’s restart RStudio. And check back, Quarto.
When you run quarto check jupyter
, you will get the same response as before, not the Python you’ve set up with, as shown in the screenshot above.
The solution
The solution is to set project-specific .Renviron
When you execute this, it will open up the editor, where you can put the following information.
QUARTO_PYTHON=/Users/enayetraheem/opt/anaconda3/envs/chasing-python/bin/python3.11
Restart
Make sure to restart your entire RStudio and then reopen the project. Then run in the terminal the
quarto check jupyter
This should render the following.
[✓] Checking Python 3 installation....OK
Version: 3.11.5 (Conda)
Path: /Users/enayetraheem/opt/anaconda3/envs/chasing-python/bin/python3.11
Jupyter: 5.5.0
Kernels: python3
[✓] Checking Jupyter engine render....OK
Summary
This article demonstrated the steps to set up a Python interpreter in a local RStudio project. We used the Anaconda Python package manager to create and manage the envs. Then we use usethis::edit_r_environ()
library with scope="project"
argument to edit the project-specific Python interpreter.
Citation
@online{raheem2023,
author = {Raheem, Enayetur},
title = {Rendering {Quarto} Document with Project-Specific {Anaconda}
{Python} Environment in {RStudio/Posit}},
date = {2023-11-20},
url = {https://www.eraheem.com/blog/2023/11/20/quarto-jupyter-conda-env-rstudio-config},
langid = {en}
}