快轉到主要內容

Virtual environments of JupyterLab, kernels, and dependencies

Make an isolated ML development environments in JupyterLab with ipykernel. Here’s how.

Create a virtual environment. Install the python libraries that you want for your ML research in that venv. Installl ipykernel in that venv as well

### Create a venv for the libraries you need

$ /Users/myname/.local/venvs/default-3.10.2/bin/python -m venv kernel-venv-3.10

$ kernel-venv-3.10/bin/python -m pip install numpy
$ kernel-venv-3.10/bin/python -m pip install ipykernel


### Then, register that venv as a IPython Kernel

$ kernel-venv-3.10/bin/python -m ipykernel install --user --name=kernel-3.10 --display-name "Python (kernel-venv-3.10)"
Installed kernelspec kernel-3.10 in /Users/myname/Library/Jupyter/kernels/kernel-3.10

$ cat /Users/myname/Library/Jupyter/kernels/kernel-3.10/kernel.json 
{
 "argv": [
  "/Users/myname/ml/kernel-venv-3.10/bin/python",
  "-m",
  "ipykernel_launcher",
  "-f",
  "{connection_file}"
 ],
 "display_name": "Python (kernel-venv-3.10)",
 "language": "python",
 "metadata": {
  "debugger": true
 }
}

Now your jupyterlab, which can be installed in another venv, can capture this kernel and show that as an option to create a notebook. Your notebook can run in an isolated, controlled enviroment!

$ jupyterlab-venv/bin/jupyter lab --notebook-dir=/Users/myname/ml/notebooks
JupyterLab creenshot that shows a new kernel which your notebook can run on
若您覺得有趣, 請 追蹤我的Facebook 或  Linkedin, 讓你獲得更多資訊!