quarto-dev / quarto-actions

GNU General Public License v2.0
228 stars 53 forks source link

quarto-dev/quarto-actions/render@v2 is not using jupyter from conda environment #101

Open eculler opened 8 months ago

eculler commented 8 months ago

I am trying to publish a site via GitHub Actions. I can render successfully on my machine using quarto cli (quarto render . and quarto publish, and when I do not include any pages with jupyter: python3 specified in the yaml header, my action works. However, when it hits a page with

---
jupyter: python3
---

in the front matter yaml it fails with:

[10/12] pages/01-reproducible-science-tools/open-reproducible-science/03-best-practices.qmd
Starting python3 kernel...Traceback (most recent call last):
  File "/opt/quarto/share/jupyter/jupyter.py", line 21, in <module>
    from notebook import notebook_execute, RestartKernel
  File "/opt/quarto/share/jupyter/notebook.py", line 17, in <module>
    import nbformat
ModuleNotFoundError: No module named 'nbformat'
Python 3 installation:
  Version: 3.10.12
  Path: /usr/bin/python3
  Jupyter: (None)
Jupyter is not available in this Python installation.
Install with python3 -m pip install jupyter
There is a environment.yml file in this directory. Is this for a conda env that you need to restore?

I installed and activated my conda environment (which include nbformat, whole environment definition here: https://github.com/cu-esiil-edu/esiil-learning-portal/blob/main/environment.yml) with the following action yaml:

jobs:
  build-deploy:
    runs-on: ubuntu-latest
    defaults:
      run:
        shell: bash -el {0}
    permissions:
      contents: write
    steps:
      ...
      - name: Create and Activate Conda
        uses: conda-incubator/setup-miniconda@v3
        with:
          activate-environment: learning-portal
          environment-file: environment.yml
          auto-activate-base: false

Using shell: bash -el {0} I have found is necessary to get the conda activation to stick for regular shell commands. And I have checked that the environment is activated and available as a kernel to shell commands with the following:

Run jupyter kernelspec list
Available kernels:
  python3    /usr/share/miniconda/envs/learning-portal/share/jupyter/kernels/python3

However, you will notice in the error above that the quarto-actions render action is using /opt/quarto/share/jupyter/jupyter.py as jupyter, and not the version from my environment.

cderv commented 8 months ago

Thanks a lot for the report. I believe we need some adjustment for Conda environment, as we have done for virtualenv to make sure the activated env works.

As a workaround, I am thinking that if you set QUARTO_PYTHON environment variable in CI jobs to the python version from your learning-portal, Quarto CLI should not do any Python auto-detection and use the right version...

Unless this is an issue only for kernel detection where first occurrence of python3 kernel found is used. 🤔

Anyhow, I am thinking we should detect CONDA_DEFAULT_ENV and if this is set, use that. Can you confirm this env var is set only when you activate a conda environment ?

Thanks!

eculler commented 8 months ago

Thanks @cderv . I've been using the QUARTO_PYTHON variable, and that works both for my GitHub Actions and also for previewing on my machine.

The following is my output for the CONDA_DEFAULT_ENV variable under different circumstances -- it appears to correspond with the active environment:

(earth-analytics-python) elsa@ESC-Macbook esiil-learning-portal % echo $CONDA_DEFAULT_ENV
earth-analytics-python
(earth-analytics-python) elsa@ESC-Macbook esiil-learning-portal % conda deactivate
(base) elsa@ESC-Macbook esiil-learning-portal % echo $CONDA_DEFAULT_ENV
base
(base) elsa@ESC-Macbook esiil-learning-portal % conda deactivate
elsa@ESC-Macbook esiil-learning-portal % echo $CONDA_DEFAULT_ENV

elsa@ESC-Macbook esiil-learning-portal %