-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpython_version.qmd
122 lines (84 loc) · 4.16 KB
/
python_version.qmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
---
title: "Python Version with Quarto"
format:
html:
code-fold: true
---
- [Issues](#issues)
- [Resolution](#resolution)
- [Earlier Command Outputs](#earlier-command-outputs)
- [References](#references)
- [Code Chunks](#code-chunks)
## Issues
The challenge I had when using Quarto was that I seem to have two versions of Python:
- Base(Python 3.12.4) ~miniconda3/bin/python
- Python 3.9.6 /Library/Developer/CommandLineTools/usr/bin/python3
I read
[references](#references)
but I don’t understand them. I know how to use the
**Python: Select Interpreter**
by hand, and that works for code chunks but not for the qmd document
(that is, I don’t know how to select interpreter for qmd files).The hint I get is that `conda` and `quarto` may not always play
well together, but I cannot understand what to do next.
I reached out to Elsa Culler, who suggested changes to `~/.zshrc`.
I further found an issue when mixing `python` and `r` code chunks.
### Resolution
- Edit `~/.zshrc` to point to conda bin
- Edit `~/.zshrc` to export `QUARTO_PYTHON`
- Change symbolic links in `~/.virtualenvs/r-reticulate/bin`
Changes to `~/.zshrc`:
> export PATH=$PATH:/opt/R/arm64/gfortran/bin:/opt/homebrew/bin\
> #export PATH=/users/brianyandell/miniconda3/bin:$PATH\
> #export PATH=/users/brianyandell/Library/Python/3.9/bin:/users/brianyandell/Library/Python/3.8/bin:$PATH\
> export QUARTO_PYTHON=/users/brianyandell/miniconda3/bin/python
Changes to `~/.virtualenvs/r-reticulate/bin`:
The issue was revealed by mixing `python` and `r` code blocks
in the document. The `r` code block simply had
> knitr::knit_exit()
This forces `quarto` to use the `~/.virtualenvs/r-reticulate/bin`, which had an old link.
[It also enables `bash` code chunks.]
I changed symbolic link of `python`
to point to the current miniconda version of python, and changed the symbolic link of `python3.9` to the older version
> `ls -l ~/.virtualenvs/r-reticulate/bin/python*`
> lrwxr-xr-x@ 1 brianyandell staff 7 Sep 9 13:28 python -> python3
> lrwxr-xr-x 1 brianyandell staff 41 Jan 17 11:49 python3 -> /Users/brianyandell/miniconda3/bin/python
> lrwxr-xr-x@ 1 brianyandell staff 58 Sep > 9 13:28 python3.9 -> /Applications/Xcode.app/Contents/Developer/usr/bin/python3
### Earlier Command Outputs
From the shell (bash or zsh), I get
> (base) brianyandell@Brians-MacBook-Pro habitatSuitability % python --version
> Python 3.12.4
If I run a code chunk from within a qmd, I get this as well. Consider this current document,
[simple.qmd](https://github.com/byandell-envsys/habitatSuitability/blob/main/simple.qmd)
The code chunk yields.
> /Users/brianyandell/miniconda3/bin/python
> 3.12.4 | packaged by Anaconda, Inc. | (main, Jun 18 2024, 10:07:17) [Clang 14.0.6 ]`
However, rendering the document (using the little icon within VSCode or from the command line with `quarto preview simple.qmd` yields
> /Library/Developer/CommandLineTools/usr/bin/python3
> 3.9.6 (default, Feb 3 2024, 15:58:27)
[Clang 15.0.0 (clang-1500.3.9.4)]
I know there is a way in the YAML to set the version, say with `jupyter: python3`.
I tried putting `python` instead of `python3` and it choked as it cannot find another kernel
> % quarto preview simple.qmd --no-browser --no-watch-inputs\
> ERROR: Jupyter kernel 'python' not found. Known kernels: python3. Run 'quarto check jupyter' with your python environment activated to check python version used.
So it clearly does not know about the other versions.
## References
I found the following references.
Note in particular the `nb_conda_kernels`.
This sounds pretty arcane.
I tried it briefly, with no success.
- [Quarto: Virtual Environments](https://quarto.org/docs/projects/virtual-environments.html)
- [Manage Jupyter Kernels in VS Code](https://code.visualstudio.com/docs/datascience/jupyter-kernel-management)
- [Quarto: Using Python: Kernel Selection](https://quarto.org/docs/computations/python.html#kernel-selection)
- [nb_conda_kernels](https://github.com/anaconda/nb_conda_kernels#use-with-nbconvert-voila-papermill)
## Code Chunks
```{python}
import sys
print(sys.executable)
print(sys.version)
```
```{bash}
ls -l ~/.virtualenvs/r-reticulate/bin
```
```{r}
knitr::knit_exit()
```