Skip to content

Instantly share code, notes, and snippets.

@Proteusiq
Last active September 13, 2021 11:43
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Proteusiq/df8ead9a40261a1f85f68a5b52c7accc to your computer and use it in GitHub Desktop.
Save Proteusiq/df8ead9a40261a1f85f68a5b52c7accc to your computer and use it in GitHub Desktop.

Data Science | miniconda

🐍 Installation

Windows and Mac users can use [GUI installer](https://docs.conda.io/en/latest/miniconda.html) . Remember to accept adding conda to PATH
Linux or Windows Subsystem Linux (wsl) can do:

# download latest miniconda and install. Remember to accept the license and adding conda to `PATH`
wget https://repo.continuum.io/miniconda/Miniconda3-latest-Linux-x86_64.sh
bash Miniconda3-latest-Linux-x86_64.sh

# Remove the script and activate conda
rm Miniconda3-latest-Linux-x86_64.sh
source ~/.bashrc

Best Practices:

  • 🌱 base environment ought to be kept as clean as possible
  • 🌱 jupyter environment to server a single instance of jupyter lab
  • 🌱 For each project, create a minimal environment

base environment

Unless a package is to be used in the global space, e.g. cookiecutter, and mamba don't install packages in base. It is the heart of miniconda. The pure it is, the easier is to upgrade and maintain other environments.

Notes: cookiecutter helps will building project templates while mamba is a superfast installer

# on base: 
conda install --channel conda-forge cookiecutter mamba 

jupyter environment

create a jupyter environment to host a single instance of jupyter lab|notebook. This will be an entry point for other envinroments that require jupyter lab

# creating jupyter env. with python 3.8
mamba create --name jupyter python=3.8 ipykernel jupyterlab ipywidgets nodejs --channel conda-forge  

conda activate jupyter
jupyter labextension install @jupyter-widgets/jupyterlab-manager jupyterlab-plotly

# adding kernel sharing with a name used to toggle env. on jupyter lab UI
python -m ipykernel install --user --name=jupyter --display-name "Jupyter"

create project environments and add kernels

use project name that echoes what the project is about. example of project names. nlp(natural langauge processing) and timeseries

mamba create --name nlp python=3.8 \
		ipykernel \
		pandas \
		scikit-learn \
		plotly \
		pytorch torchvision torchaudio cudatoolkit=11.1 \
		transformers \
		--channel pytorch \
		--channel huggingface \
		--channel plotly

conda activate nlp
python -m ipykernel install --user --name=nlp --display-name "NLP"
conda deactivate

we can select different versions of pythons and use pip install in the activated environment

mamba create --name timeseries python=3.7 ipykernel pandas scikit-learn plotly --channel plotly

conda activate timeseries
# in activated project environment, you can use `pip install` e.g. `pip install -r requirements.txt`
pip install sktime watermark
python -m ipykernel install --user --name=timeseries --display-name "TimeSeries"
conda deactivate

To use our created environments in Jupyter Lab, activate jupyter environment and launch jupyter lab in the project folder.

cd documents/stocksanalysis/notebooks
conda activate jupyter
jupyter lab --port 8080

This will serve jupyter lab UI on localhost port 8080. In Jupyter Lab UI, toggle onto the correct environment, e.g. TimeSeries.

🐍 Extras

list available environments

conda env list

remove environment

mamba env remove --name <project_name>

list available kernels

jupyter kernelspec list

remove a kernel project_name

jupyter kernelspec remove <project_name>

export environments

# export
conda activate nlp
conda env export | grep -v "^prefix: " > environment.yml

# import
conda env create -f environment.yml
conda activate nlp

list packages in an envinronment

conda list -n <env_name>

install or uninstall package in an environment

# install httpx from conda-forge channel in timesries environment
mamba install --name timeseries httpx --channel conda-forge

# uninstall pandas from nlp environment
mamba remove --name nlp pandas
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment