Skip to content

Instantly share code, notes, and snippets.

@rxm
Last active Jun 9, 2019
Embed
What would you like to do?
Local Python package installs on the macOS

On my laptop I like to keep my Python packages installed in my account. This way I avoid overwriting something the system may need. For that to work I need to tell Python where to find the packages, which is done by setting a few Python environment variables.

For other ways of having local versions of Python, see:

Create the the site-packages folder with

mkdir -p $HOME/Library/Python/2.7/lib/python2.7/site-packages

and then add this to your Bash profile, the ~/.profile

# add python stuff
# call to add Python stuff
addPython() {
  echo $PATH| tr ':' '\n' | grep -q 'Library/Python/2.7/bin'
  if [[ $? -ne 0 ]]; then
     PYTHONPATH=$PYTHONPATH:$HOME/Library/Python/2.7/lib/python2.7/site-packages
     PATH=$PATH:$HOME/Library/Python/2.7/bin
     PYTHONUSERBASE=$HOME/Library/Python/2.7
     export PYTHONPATH
     export PYTHONUSERBASE
  fi
}
addPython

With this done, easy_install can be used to install pip

source ~/.profile
easy_install --prefix=$HOME/Library/Python/2.7 pip

and then pip can be used to install other things, such as

pip install --user  jinja2 

The --user flag uses the conventions adopted in PEP370 on how to setup Python locally. The --user flag follows those conventions.

Things installed with pip install --user will go into ~/Library/Python/2.7. To install, for example, the awscli package use:

 pip install --user awscli
 
 # upgrade with
 pip install --user --upgrade awscli

If an upgrade fails, that could be because of TLSv1 issues. To upgrade issue

curl https://bootstrap.pypa.io/get-pip.py | python - --user

to pass the --user flag to the Pip installer script.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment