Skip to content

Instantly share code, notes, and snippets.

@jnmclarty
Last active March 3, 2018 20:58
Show Gist options
  • Save jnmclarty/427e6d1645dbd24ca9b39fdcfdb7738b to your computer and use it in GitHub Desktop.
Save jnmclarty/427e6d1645dbd24ca9b39fdcfdb7738b to your computer and use it in GitHub Desktop.
How to Set up Python w/ a Seperate Anaconda for Multiple-Projects on Ubuntu 16.04.1 LTS x64

1. Setup Ubuntu-level Stuff for a Python Dev

apt-get update
adduser jnmclarty
usermod -aG sudo jnmclarty
su - jnmclarty
sudo apt-get -y upgrade
sudo apt-get install make build-essential libssl-dev libffi-dev python-dev python-virtualenv zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev libncursesw5-dev xz-utils
nano ~/.bash_profile

add:

if [ -f ~/.bashrc ]; then
   source ~/.bashrc
fi

2. Install pyenv

curl -L https://raw.githubusercontent.com/yyuu/pyenv-installer/master/bin/pyenv-installer | bash

Despite the instructions saying to add to ~/.bash_profile

nano ~/.bashrc  

add:

export PATH="/home/jnmclarty/.pyenv/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"

Restart the session.

Check:
python --> Python 2.7.12
python3 --> Python 3.5.2
pyenv --> Should show commands

3. Install Anaconda

wget https://repo.continuum.io/archive/Anaconda3-5.1.0-Linux-x86_64.sh
bash Anaconda3-5.1.0-Linux-x86_64.sh
Do you approve the license terms? [yes|no]
>>> 'yes'
[/home/you/anaconda3] >>> enter
Do you wish the installer to prepend the Anaconda3 install location
>>> 'yes'

This adds to ~/.bashrc:

# added by Anaconda3 4.2.0 installer
export PATH="/home/jnmclarty/anaconda3/bin:$PATH"

Comment out the export, leave it there for explicitness

nano ~/.use_anaconda

add:

export PATH="/home/jnmclarty/anaconda3/bin:$PATH"

Enjoy

Commands

(Each section is from fresh shell)

pyenv

pyenv
pyenv install --list
pyenv install 3.6.0
pyenv install 2.7.13
pyenv install 3.5.2
pyenv versions

Switch App-level

pyenv local 3.6.0 2.7.13
python
/home/jnmclarty/.pyenv/versions/3.6.0/lib/python36.zip
/home/jnmclarty/.pyenv/versions/3.6.0/lib/python3.6
/home/jnmclarty/.pyenv/versions/3.6.0/lib/python3.6/lib-dynload
/home/jnmclarty/.pyenv/versions/3.6.0/lib/python3.6/site-packages
python2
python3
python2.7

Switch back

pyenv local system

pyenv & virtualenv

pyenv virtualenv 2.7.13 my-virtual-env-2.7.13
pyenv local my-virtual-env-2.7.13
pip install flask
python
>>> import flask
>>> flask.__file__
'/home/jnmclarty/.pyenv/versions/my-virtual-env-2.7.13/lib/python2.7/site-packages/flask/__init__.pyc'

Anaconda

. ~/.use_anaconda3
python
/home/jnmclarty/anaconda3/lib/python35.zip
/home/jnmclarty/anaconda3/lib/python3.5
/home/jnmclarty/anaconda3/lib/python3.5/plat-linux
/home/jnmclarty/anaconda3/lib/python3.5/lib-dynload
/home/jnmclarty/anaconda3/lib/python3.5/site-packages
/home/jnmclarty/anaconda3/lib/python3.5/site-packages/Sphinx-1.4.6-py3.5.egg
/home/jnmclarty/anaconda3/lib/python3.5/site-packages/setuptools-27.2.0-py3.5.egg

Create conda environment

. ~/.use_anaconda3
conda create -n my27 python=2.7
source activate my27
/home/jnmclarty/anaconda3/envs/my27/lib/python27.zip
/home/jnmclarty/anaconda3/envs/my27/lib/python2.7
/home/jnmclarty/anaconda3/envs/my27/lib/python2.7/plat-linux2
/home/jnmclarty/anaconda3/envs/my27/lib/python2.7/lib-tk
/home/jnmclarty/anaconda3/envs/my27/lib/python2.7/lib-old
/home/jnmclarty/anaconda3/envs/my27/lib/python2.7/lib-dynload
/home/jnmclarty/anaconda3/envs/my27/lib/python2.7/site-packages
/home/jnmclarty/anaconda3/envs/my27/lib/python2.7/site-packages/setuptools-27.2.0-py2.7.egg

Run ("System"-like) 2.7.12 (usr/bin/python2.7)

python
/usr/lib/python2.7
/usr/lib/python2.7/plat-x86_64-linux-gnu
/usr/lib/python2.7/lib-tk
/usr/lib/python2.7/lib-old
/usr/lib/python2.7/lib-dynload
/usr/local/lib/python2.7/dist-packages
/usr/lib/python2.7/dist-packages

Run "System" 3.5.2 (usr/bin/python3.5)

python3
/usr/lib/python35.zip
/usr/lib/python3.5
/usr/lib/python3.5/plat-x86_64-linux-gnu
/usr/lib/python3.5/lib-dynload
/usr/local/lib/python3.5/dist-packages
/usr/lib/python3/dist-packages

References

  • pyenv – A Python version manager. Installs different versions and flavors of Python interpreters.
  • pyvenv – A tool to create isolated virtual environments from a Python interpreter. Ships with Python from 3.4.
  • virtualenv – Creates virtual environments, available in PyPi.
  1. http://masnun.com/2016/04/10/python-pyenv-pyvenv-virtualenv-whats-the-difference.html
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment