Skip to content

Instantly share code, notes, and snippets.

@rxm
Last active June 9, 2019 15:52
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rxm/0c3eaade72ed45587d908dd18a3e2195 to your computer and use it in GitHub Desktop.
Save rxm/0c3eaade72ed45587d908dd18a3e2195 to your computer and use it in GitHub Desktop.
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