Skip to content

Instantly share code, notes, and snippets.

@fhirschmann
Last active June 12, 2016 15:35
Show Gist options
  • Save fhirschmann/056e8ec9f74f560b88e6ba015f9d0933 to your computer and use it in GitHub Desktop.
Save fhirschmann/056e8ec9f74f560b88e6ba015f9d0933 to your computer and use it in GitHub Desktop.
#!/bin/sh
# Define packages to install. For exemplary purposes, this is done
# in this script instead of a separate file.
PACKAGE_FILE=$(mktemp)
cat << EOF > $PACKAGE_FILE
sympy
flask
EOF
# Install Python 2 and 3 globally from a single Conda distribution (e.g. Anaconda 3 only)
# Two installations of Anaconda are never needed, the only difference between Anaconda 2 and 3
# is that it defaults to Python 2 and 3, respectively.
sudo conda create -y -n python3 --file $PACKAGE_FILE python=3 anaconda
sudo conda create -y -n python2 --file $PACKAGE_FILE python=2 anaconda
# Install global default Python kernels
source activate python2 && sudo ipython kernel install
source activate python3 && sudo ipython kernel install
# Use case 1:
# The user starts Jupyter Notebook and can choose the default installation of Python 2 or 3
# Current environments
conda info --envs
#python2 /usr/local/anaconda3/envs/python2 (1.5G)
#python3 /usr/local/anaconda3/envs/python3 (849M)
# Use case 2:
# The user wishes to install Python packages and creates her or his personal env
conda create -y -n python3 --clone python3
conda info --envs
#python3 * /home/fabian/.conda/envs/python3 (163M)
#python2 /usr/local/anaconda3/envs/python2 (1.5G)
#python3 /usr/local/anaconda3/envs/python3 (849M)
# The personal python3 environment supersedes the global environment
# and is either hard or softlinked to minimize disk space.
source activate python3 && ipython kernel install --user
# The kernel was installed into ~/.local/share/jupyter/kernels/python3
# and is preferred over the kernel installed in line 21.
# Now, open jupyter notebook and start a Python 2 and 3 notebook side by side.
# Type `import sys; sys.path` simultaneously.
# Because Python 3 was installed by the user, the paths are user-dependant
# Work can continue as usual from here, e.g.
source activate python3 && conda install sphinx -y
# Does it work in PyCharm?
# Yes. https://www.jetbrains.com/help/pycharm/2016.1/adding-existing-virtual-environment.html
###################################################################################
# TIP: Use project-specific environments
# Note: This does not use mentionable additional disk space
conda create -y -n projectx --clone python3
source activate projectx && conda list -e > requirements.txt
# New project members can now install their environments using
# conda create -y -n projectx --clone python3 --file requirements.txt
# Install a project-specific IPython kernel for the Jupyter dropdown menu
source activate projectx && ipython kernel install --user --display-name projectx
# Discussion: In line 30, we overloaded the environment with name 'python3'. If the user clones
# 'python3', the personal configuration (packages...) will be part of the new project-specific
# environment when cloned with --clone python3.
#
# Possible solution: choose -n mypython3 in line 30 and leave line 40 as is. Then, the dropdown
# menu in Jupyter will choose the 'mypthon3' environment when selected because line 40 installs
# using the default display name 'Python 3', which supersedes the default kernel specification
# installed in line 19.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment