-
Notifications
You must be signed in to change notification settings - Fork 26
Environment management with `virtualenv`
One way to set up your python development environment on OS X is to use virtualenv. This allows you to have separate installations of python packages for various development scenarios and to develop using both python 2 and python 3 as needed.
Basic pattern: Use brew to install python. Use the pip installed by brew to install python packages that should be globally available. Use pip within a virtualenv to install python packages within that virtualenv.
- Install python and virtualenv:
brew install python
brew install python3
pip install virtualenvQ: Why install python 2 when it comes bundled with the os?
A: It is simple to keep the version you install up to date with brew. Also, apparently Apple has made their own changes to the bundled python, so you really don't want to rely on it to behave like python on other systems. Also apparently, upgrading OS X can mess up the bundled python.
- To prevent you from accidentally installing a python package outside of a
virtualenvunless you really mean to, update your~/.bashrcwith the following lines:
# Only install python packages in virtualenv
export PIP_REQUIRE_VIRTUALENV=true
# Unless you really want to install python package globally
gpip(){
PIP_REQUIRE_VIRTUALENV="" pip "$@"
}- Reload to get the updates:
source ~/.bash_profile- Create a directory for your virtual environments. Create this directory as a peer of your development workspace, e.g. your 'git' or 'projects' directory. The rest of this document will assume that your project directory is ~/git and your virtual environment directory is ~/venvs.
mkdir ~/venvs- Create and activate some virtual environments to understand how it works:
To create and activate a python 2.7 virtualenv named py2-scripts:
cd ~/venvs
virtualenv --prompt py2-scripts- py2-scripts
source ~/venvs/py2-scripts/bin/activateTo create and activate a python 3 virtualenv named py3-proj:
cd ~/venvs
virtualenv --python python3 --prompt py3-proj- py3-proj
source ~/venvs/py3-proj/bin/activateTo leave an active virtualenv:
deactivate- Install
virtualenvwrapper. Be sure to run this command outside of anyvirtualenv:
deactivate
gpip install virtualenvwrapper- Add the following lines to your
~/.bashrc:
# virtualenvwrapper
export WORKON_HOME=$HOME/venvs
export PROJECT_HOME=$HOME/git
source /usr/local/bin/virtualenvwrapper.sh- Reload
~/.bashrc:
source ~/.bashrc- List your
virtualenvs:
workon- Activate an existing
virtualenvand associate a project directory with it. Assuming that your project directory for thepy2-scriptsvirtualenvis~/git/some-old-scripts:
workon py2-scripts
cd ~/git/some-old-scripts
setvirtualenvprojectNow, when you workon py2-scripts, your shell will automatically change to your project directory.
- Associate a project directory with your other
virtualenv:
deactivate
setvirtualenvproject ~/venvs/py3-proj ~/git/new-cool-project- Going forward, you can create a
virtualenvand a project directory with the same name at the same time:
mkproject [virtualenv opts] myprojectOr create a virtualenv for an existing project:
mkproject -f [virtualenv opts] myproject