quarto-dev / quarto

Quarto open-source scientific and technical publishing system
https://quarto.org
GNU Affero General Public License v3.0
332 stars 29 forks source link

Ctrl+Shift+K: rendering QMD files in the `base` python environment. #112

Closed lux5 closed 1 year ago

lux5 commented 1 year ago

I had conda installed as the package manager. I have got multiple environments. But when I use the shortcut key to render my QMD file, it seems quarto always activate my base env, and in most cases, it failed because I didn't install any extra modules under the base env. I could manually activate the preferred env, and type the render CMD there. But I was wondering if I could set up a specific env for rendering my QMD files when I press Ctrl + Shift + K?

The simple file I need to render is like this:


title: "Run R/Python codes in the same notebook" format: html

import pandas as pd
pd.DataFrame({'A': [1, 2], 'B': [3, 4]})
library(data.table)
data.table(A = c(1, 2), B = c(3, 4))
jjallaire commented 1 year ago

Quarto will use whatever you have defined as the default Python interpreter for the current workspace. There is a Python: Select Default Interpreter command you can use per-workspace and there is a Python Default Interpreter Path setting you can use to provide a global default.

lux5 commented 1 year ago

Quarto will use whatever you have defined as the default Python interpreter for the current workspace. There is a Python: Select Default Interpreter command you can use per-workspace and there is a Python Default Interpreter Path setting you can use to provide a global default.

Thank you for the hint. I will read the documentation aboiut this part.

lux5 commented 1 year ago

I refer to - https://quarto.org/docs/projects/environment.html#overview, and find

Provide options for language interpreters (e.g. PYTHONINSPECT or TZDIR).

I added a file _environment in the same folder as my qmd file, with the following:

PYTHONINSPECT: 'C:/Users/lux/mambaforge/envs/da/python.exe'

But it still produced the same error. I was wondering if you could provide an simple example what to do? I have no idea where I could find the documents on this. Many thanks.

jjallaire commented 1 year ago

Here is the documentation for configuring Python interpreters in VS Code: https://code.visualstudio.com/docs/python/environments

Once you have setup your default Python interpreter (either global or for a workspace) then Quarto will use it.

Note that PYTHONINSPECT has nothing to do with Python versions or paths.

lux5 commented 1 year ago

Python Default Interpreter Path

Sorry for the late reply, and thank you for telling me the right documentation. After searching setting up the default python inter for my workspace. I still couldn't figure out how to make this work. I will show what I did and where I got my problem is as follows:

  1. I created a new project, and in the 'test.qmd' file, the contents are:

title: "Run R/Python codes in the same notebook" format: html

import pandas as pd
pd.DataFrame({'A': [1, 2], 'B': [3, 4]})
library(data.table)
data.table(A = c(1, 2), B = c(3, 4))
  1. I have created a 'setting.json' file in my project folder, and the contents are:
{
    "python.defaultInterpreterPath": "C:\\Users\\lux\\mambaforge\\envs\\da\\python.exe",
    "python.terminal.activateEnvironment": true,
}
  1. When I used the shortcut keys Ctrl + Shift + K, I got the same error (as shown in the attached snapshot, Quarto is still using my base environment). But I run import pandas as pd from my py file, it ran under the correct environment da with no error. I was wondering if you could point out what I did wrong, and how to make this work. Many thanks!

Capture

lux5 commented 1 year ago

This small example produced the same error even though the latest quarto is installed:

> quarto --version
1.3.313

I was wondering if anyone could help with this?

jjallaire commented 1 year ago

I think the problem is that when you are using R and Python in the same file R is actually calling Python so the normal rules about binding Python environments don't apply. You will probably need the use_python() function to point the way to R: https://rstudio.github.io/reticulate/articles/r_markdown.html

lux5 commented 1 year ago

I think the problem is that when you are using R and Python in the same file R is actually calling Python so the normal rules about binding Python environments don't apply. You will probably need the use_python() function to point the way to R: https://rstudio.github.io/reticulate/articles/r_markdown.html

Thank you so much for the hint (about the engine setup). Eventually, following your hint, I figured this out by put this at the beginning of my notebook:

---
title: "Run R/Python codes in the same notebook"
format: html
engine: jupyter
---
jjallaire commented 1 year ago

Great! So happy to hear that it is working.

brbarkley commented 1 year ago

Hello - the identified solution here works for Ctrl+Shift+K to render qmd files in VS Code via the Quarto Extension or the cli. However, if executing interactively with the Run Cell buttons in VS Code, python chunks are sent to the python interactive window and R chunks are sent to the R Extension interactive window (as described in https://github.com/quarto-dev/quarto-cli/issues/2313; also see https://github.com/quarto-dev/quarto/issues/77 and https://github.com/REditorSupport/vscode-R/issues/1299).

Are there any plans, if using the knitr engine in vs code, to allow python chunks to be sent to the R Extension interactive window via reticulate::repl_python()?

Would be a nice feature and would bring quarto in VS Code closer to parity with quarto in RStudio.

jjallaire commented 1 year ago

Just implemented support for reticulate execution here: https://github.com/quarto-dev/quarto/commit/5b1e799aae821798d28e24e0dddca0fa31544884

This will be an updated release available later this week.