What is the output of which python
?
If /usr/bin/python
then using the system python that comes with OSX. 😬
What is the output of (should be nearly empty):
/Library/Python/2.7/site-packages
If there is a lot of content there, 😬, this means you install stuff using sudo.
Will need homebrew (which needs xcode's commandline tools).
Install commandline tools: xcode-select --install
Install homebrew: https://brew.sh/
If already using homebrew
Make sure all is well:
brew doctor
Fix any glaring errors.
ls -al $(brew --prefix)
If there are folders that owned by root, then do:
sudo chown -R $(whoami):staff $(brew --prefix)/*
mkdir /usr/local/Frameworks
- Install multiple versions of python
- Switch python versions with ease (or no effort at all)
- Install python packages without having to sudo
- Use different python requirements for different projects
Install python 2 & 3:
brew install pyenv
pyenv install 3.6.4
pyenv install 2.7.14
# activate 3.6.4 as the default
pyenv global 3.6.4
# see what versions installed
pyenv versions
Note version system
is the one to avoid and keep vanilla.
Install virtualenv plugin for pyenv:
brew install pyenv-virtualenv
Example, helios:
cd helios/
# create home for libs
pyenv virtualenv helios
# activate the virtualenv
pyenv local helios
# install project libs
pip install --upgrade pip
Verify with:
$ pyenv which python
/Users/doug/.pyenv/versions/helios/bin/python
Install deps:
pip install -r requirements.txt
Notice there a .python-version
file, this will be used to auto-activate the venv.
For autocompletion add to bash profile:
eval "$(pyenv init -)"
Now is a good time to add .python-version
to global ignore at ~/.gitignore
And should now be able to easily run:
pip install -r requirements.txt
And not clash or pollute any other project or python install.
Add to bash profile some helpers:
# pyenv (installed via homebrew)
eval "$(pyenv init -)"
create_and_use_ve () {
(set -x
# creates a new venv in ~/.pyenv/versions
pyenv virtualenv ${PWD##*/}
# creates .python-version in directory
pyenv local ${PWD##*/}
pip install --upgrade pip
)
}
alias venv=create_and_use_ve
destroy_ve () {
(set -x
pyenv uninstall -f ${PWD##}
rm .python-version
cd .
)
}
alias deactivate=destroy_ve
And now, to create a virtual env the same name as the current directory:
venv
And to completely remove the venv:
deactivate