Skip to content

Instantly share code, notes, and snippets.

@quidome
Last active September 12, 2017 20:13
Show Gist options
  • Save quidome/c9f3e828c2231c6667adbcba5c1d0bfe to your computer and use it in GitHub Desktop.
Save quidome/c9f3e828c2231c6667adbcba5c1d0bfe to your computer and use it in GitHub Desktop.
Installing python and virtualenvwrapper in OSX Sierra

Installing python and virtualenvwrapper in OSX Sierra

I don't want to change system python and have the freedom to run python2 or python3 scripts with all needed modules. At the moment virtualenv seems the solution for me.

Homebrew

Follow instructions from [https://brew.sh/]

Install python

When homebrew work we can install python from brew:

# brew install python python3

During installation some handy stuff is printed, like adding the python path to $PATH.

Update modules for both installs

Python2

# pip2 install --upgrade pip setuptools

And python3

# pip3 install --upgrade pip setuptools wheel

Virtualenv

Install virtualenv and wrapper in the python2 environment

# pip2 install virtualenv virtualenvwrapper

Create virtualenv directory

# mkdir $HOME/.virtualenvs

Add the lines below to .bashrc or some other file which is being sourced

VIRTUALENVWRAPPER_PYTHON=$(which python2)
export WORKON_HOME=~/.virtualenvs
source /usr/local/bin/virtualenvwrapper.sh

# uncomment following lines to load a virtualenv when opening a shell
#if (tty -s); then
#  workon py3
#fi
# source .bashrc

Create virtualenv

Let's just start by creating a new python3 environment

# mkvirtualenv -p python3 py3

PIP

By adding some configuration we can make it harder to install modules in the global environment. Add the following to your pip confguration $HOME/Library/Application\ Support/pip/pip.conf

[install]
require-virtualenv = true

[uninstall]
require-virtualenv = true

The following functions could be used to change the global environment when needed:

gpip2(){
   PIP_REQUIRE_VIRTUALENV="" pip2 "$@"
}
gpip3(){
   PIP_REQUIRE_VIRTUALENV="" pip3 "$@"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment