Skip to content

Instantly share code, notes, and snippets.

@banditkings
Last active May 16, 2024 06:14
Show Gist options
  • Save banditkings/606a9924bf7d9f3e62c2b9541a32cca8 to your computer and use it in GitHub Desktop.
Save banditkings/606a9924bf7d9f3e62c2b9541a32cca8 to your computer and use it in GitHub Desktop.
Getting quarto to use the right python version and virtual environment

Quarto with Python, Poetry, and VSCode

If you're having issues with getting pyenv + poetry + quarto + vscode to work together, the solution is to set the QUARTO_PYTHON environment variable to the location of the specific python version you want to use.

For instance, in bash we'd do the following within the project directory:

poetry shell
export QUARTO_PYTHON=$(which python)

But rather than do it one-off you should instead set the QUARTO_PYTHON env variable in your .bashrc or .zshrc file, i.e.:

export QUARTO_PYTHON=/Users/your_username/Library/Caches/pypoetry/virtualenvs/quarto-book-5nMdc-E9-py3.10/bin/python

Alternatively you could always go into your project directory and run the quarto commands manually in the terminal within your active virtual environment, but explicitly setting the QUARTO_PYTHON env variable makes things easier and the VSCode quarto extension will work properly.

VSCode

I noticed that if you hit the 'preview' button on VSCode (with the quarto extension) on a .qmd document, it would open up a new terminal window and if you didn't have a QUARTO_PYTHON environment variable set, it would actually set one for you based on your global pyenv. So if your global pyenv was set to 3.10.13 but your local pyenv was 3.10.6, it'll probably fail. This isn't an issue with the interactive mode (where you can pick the jupyter kernel you want to use) but that 'preview' button seems to be making some assumptions about your setup that might be wrong.

Multiple kernels in the same environment

If you have more than one jupyter kernel installed, you may need to specify the specific kernel you want to use in a .qmd file by specifying the kernelspec in the header:

---
title: "Quarto in Poetry"
jupyter: 
  kernelspec:
    name: "python3"
    language: "python"
    display_name: "Python 3 (ipykernel)"
---
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment