Skip to content

Install python, virtualenv, virtualenvwrapper in a brew environment

Michael Hulse edited this page Feb 6, 2015 · 3 revisions

Update/related info here: Django local dev

TL;DR: I now use Python 3.4.x and pyvenv, so, ignore any mention of virtualenv(wrapper) in my WIKI page(s).


What we did to get a clean install of python, virtualenv and virtualenvwrapper (somehow, we both ended up with multiple Pythons installed — and I had multiple virtualenvs!):

$ pip uninstall virtualenvwrapper
# And again, until it says there are no virtualenvwrappers installed ... 
$ pip uninstall virtualenvwrapper
$ pip uninstall virtualenv
# And again, until it says there are no virtualenvs installed ... 
$ pip uninstall virtualenv

$ brew uninstall python
# And again, until it says there're no pythons installed ... 
$ brew uninstall python

* and then do ... *

# I've seen 'brew remove ... ' work and 'brew uninstall ... ' do nothing
$ brew remove python
# And again, until it says there're no pythons installed ... 
$ brew remove python

# Now, we build it back up ... 
$ brew install python
$ pip install virtualenv
$ pip install virtualenvwrapper

Note that virtualenvwrapper.sh has moved:

$ source /usr/local/share/python/virtualenvwrapper.sh

$ source /usr/local/bin/virtualenvwrapper.sh

See https://virtualenvwrapper.readthedocs.org/en/latest/install.html?highlight=workon_home#shell-startup-file for shell setup details.

So now you can:

$ mkvirtualenv foo

cd to where I want to work ...

(foo)$ git clone [email protected]:jheasly/django_obit_desk2.git
(foo)$ cd django_obit_desk2
(foo)$ pip install -r requirements.txt

Micky's version

We'll consolidate instructions later.

Install Python 2.x using brew (see John's version above as well):

$ brew install python

Before installation, check your python version and path for comparison's sake.

Details here:

Homebrew and Python

Cool factor: pip comes with brew install python! Nice!

Now you could check/compare stuff by running:

$ which python
/usr/local/bin/python

The above is the brew python.

... or:

$ python
Python 2.7.4 (default, May 17 2013, 13:36:15) 
[GCC 4.2.1 Compatible Apple LLVM 4.2 (clang-425.0.28)] on darwin
Type "help", "copyright", "credits" or "license" for more information.

... which will start the interactive python interpreter and show you the version number (among other things).

Also, this:

$ brew info python

... will give you lots of details as well.

It's also good to know that the order of profile-related files loads in this order:

~/.bash_profile
~/.bash_login
~/.profile

... whichever one is found first will be used by the shell.

Check out Bashstrap for some good tips on how to setup your dot bash files.

Install virtualenv and virtualenvwrapper.

Some good instructions found here:

Installing Pip, Virtualenv & VirtualenvWrapper on OS X

pip install virtualenv 

Once done, you can see if it's installed via:

$ pip freeze

You can also type:

$ which virtualenv
/usr/local/bin/virtualenv

... or:

$ virtualenv
You must provide a DEST_DIR
Usage: virtualenv [OPTIONS] DEST_DIR
...

Next, install virtualenvwrapper:

pip install virtualenvwrapper

Setup your virtualenvs folder.

cd; mkdir .virtualenvs

Add these lines to your .bash_profile:

# Python virtual environments:
# https://github.com/registerguard/registerguard.github.com/wiki/Install-python,-virtualenv,-virtualenvwrapper-in-a-brew-environment
export WORKON_HOME=$HOME/.virtualenvs
#export WORKON_HOME=/tmp/foo/.virtualenvs
export VIRTUALENVWRAPPER_VIRTUALENV_ARGS='--no-site-packages'
export PIP_VIRTUALENV_BASE=$WORKON_HOME
export PIP_RESPECT_VIRTUALENV=true
if [[ -r /usr/local/bin/virtualenvwrapper.sh ]]; then
    source /usr/local/bin/virtualenvwrapper.sh
else
    echo "WARNING: Can't find virtualenvwrapper.sh"
fi

Open a new terminal window and type:

$ mkvirtualenv

... to see if everything is setup properly.

Now let's create our virtual environment!

I decided to call mine django_1_3, so I can run multiple versions of Django and keep them organized by name.

$ mkvirtualenv --no-site-packages django_1.3
New python executable in django_1.3/bin/python
Installing setuptools............done.
Installing pip...............done.

WARNING If you have a space in your HDD, you'll have to edit ~/.virtualenvs/django_1.3/bin/activate and put a \ before the space in your HDD name.

You should already be in the django_1_3 virtual environment, but here's how you navigate between envs:

$ workon # Should list all available environments.
$ workon ENVIRONMENT_NAME_HERE # Activates desired env.
$ deactivate # Exits current env.

Next, install Django 1.3.x (make sure you're working on an env):

pip install django==1.3.1

... that will install django 1.3.x into the django_1_3 env.

Now, create a development folder somewhere on your machine. I put mine here:

~/development

... cd to this location and run:

$ django-admin.py startproject django_1_3

... that will create a new Django project called django_1_3.

Symlink your GitHub apps.

I put my GitHub repos here:

~/mhulse/github

Since my Django test project is elsewhere, I symlink the repo's app like so:

$ ln -s ~/github/registerguard/django-ad-manager/ad_manager/ ad_manager

... that creates a symbolic link from my prefered GitHub repository folder to the Django project's folder. This allows me to keep my GitHub boilerplate separate from my Django stuff.

Welcome to Django land!

Now that the app is in your Django app, and you're virtual env is setup with your desired Django version, you can tweak your settings file.

Add ad_manager to your INSTALLED_APPS setting.

Setup your DB.

See here:

https://gist.github.com/mhulse/1954422

SQL Lite is easy one to get going with, so here's one option:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
        'NAME': 'django_1_3',                   # Or path to database file if using sqlite3.
        'USER': '',                             # Not used with sqlite3.
        'PASSWORD': '',                         # Not used with sqlite3.
        'HOST': '',                             # Set to empty string for localhost. Not used with sqlite3.
        'PORT': '',                             # Set to empty string for default. Not used with sqlite3.
    }
}

The rest is up to you!

Clone this wiki locally