jupyter pyenv and virtualenv in macOS

Conbined with pyenv virtualenv and Jupyter kernal specification, we could have different versions of project-specific python wrapper as below.

This article includes needed information on enabling this.

pyenv

ref: https://github.com/pyenv/pyenv/issues/1219

zlib not available, one thing to highlight is to enable zlib, we have to do this:

1
2
xcode-select --install
sudo installer -pkg /Library/Developer/CommandLineTools/Packages/macOS_SDK_headers_for_macOS_10.14.pkg -target /

Below are things we need to ensure pyenv magically modified our enviroment variables to point specific version of python.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
$ brew install pyenv
$ cd <project_path>
$ pyenv install 3.6.8
$ pyenv local 3.6.8
$ pyenv init
# Load pyenv automatically by appending
# the following to ~/.zshrc:

eval "$(pyenv init -)"

$ which python
/usr/local/bin/python
$ eval "$(pyenv init -)"

$ which python
/Users/<user>/.pyenv/shims/python

jupyter/IPython kernel management

ref: https://ipython.readthedocs.io/en/latest/install/kernel_install.html

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
$ jupyter kernelspec list
Available kernels:
python3 /usr/local/share/jupyter/kernels/python3

$ python -m virtualenv py36

$ source py36/bin/activate

$ pip install ipykernel

$ python -m ipykernel install --user --name py36 --display-name "ProjectName py36"

$ jupyter kernelspec list
Available kernels:
py36 /Users/<user>/Library/Jupyter/kernels/py36
python3 /usr/local/share/jupyter/kernels/python3

Then, from the jupyter notebook webpage, you could select kernels from Kernel: Change Kernel

matplotlib calling latex failed on macOS

plot call failed due to latex could not be invoked

Root cause

LaTeX was not installed or cannot be called fromlatex

Minimal reproducing

1
2
3
4
5
6
7
8
>>> import subprocess; subprocess.check_call("latex")

# FileNotFoundError: [Errno 2] No such file or directory: 'latex': 'latex'

# or from shell

$ latex
# latex not found

Fix

  • install Tex Live for macOS
  • append PATH for /Library/TeX/texbin
1
2
3
4
5
6
7
8
$ wget http://tug.org/cgi-bin/mactex-download/MacTeX.pkg
# checksum and install MacTeX.pkg

# assuming you are using zsh
$ echo 'export PATH="$PATH:/Library/TeX/texbin"' >> ~/.zshrc

# assuming you are using bash
$ echo 'export PATH="$PATH:/Library/TeX/texbin"' >> ~/.bashrc

Verify it

1
2
3
4
5
6
7
8
9
10
11
12
13
14
$ export PATH="$PATH:/Library/TeX/texbin"
$ latex --version
pdfTeX 3.14159265-2.6-1.40.19 (TeX Live 2018)
kpathsea version 6.3.0
Copyright 2018 Han The Thanh (pdfTeX) et al.
There is NO warranty. Redistribution of this software is
covered by the terms of both the pdfTeX copyright and
the Lesser GNU General Public License.
For more information about these matters, see the file
named COPYING and the pdfTeX source.
Primary author of pdfTeX: Han The Thanh (pdfTeX) et al.
Compiled with libpng 1.6.34; using libpng 1.6.34
Compiled with zlib 1.2.11; using zlib 1.2.11
Compiled with xpdf version 4.00