Conda Utils & Snippets

Outline

Virtual Environment

Everything starts w/ an independent virtual environment. Install conda will make it easier.

After installation, it might be necessary to add this line to .bashrc or .zshrc:

export PATH="${PATH_TO_ANACONDA}/bin:$PATH"

To create a virtual environment:

conda create -n lb python=3 numpy pandas

To activate the virtual environment:

conda activate lb

To install more packages to virtual environment:

conda install -n lb matplotlib

To export an environment configuration:

conda activate myenv
conda env export > environment.yml

To recreate a venv w/ given environment.yml:

conda env create -f environment.yml

To delete a virtual env:

conda remove -n ${myenv} --all

or:

conda env remove --name ${myenv}

Reference: conda wiki page

Jupyter Notebook

To install jupyter notebook:

conda install jupyter notebook

To easily manage conda virtual environment in jupyter notebook, install nb_conda:

conda install nb_conda

To install virtual environment into ipykernel jupyter notebook, run:

python -m ipykernel install --user --name lb --display-name "Python3 (lb)"

To remove virtual environment from ipykernel jupyter notebook:

# notice ${unwanted-kernel} should be name instead of display name
jupyter kernelspec uninstall ${unwanted-kernel}

To open jupyter notebook w/o browser and do port forwarding stuffs:

jupyter notebook --no-browser --port=9000

To get access to this jupyter notebook from laptop:

ssh -N -L localhost:9000:localhost:9000 [email protected] & sleep 0.5; open -a Safari http://localhost:9000

Then the ssh job will be running in the background. To list all jobs, just run jobs. In case you want to kill the job, get its id and run kill %{ssh_job_id}. For more info on CLI job, go here.

To convert a notebook to an HTML file:

jupyter nbconvert --to html notebook.ipynb

Jupyter nbextensions:

conda install -c conda-forge jupyter_contrib_nbextensions