Skip to content

Instantly share code, notes, and snippets.

@beck
Last active July 24, 2018 06:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save beck/a79d54f5bda49b0766a43366c603bf5d to your computer and use it in GitHub Desktop.
Save beck/a79d54f5bda49b0766a43366c603bf5d to your computer and use it in GitHub Desktop.
Managing Python

Managing Python

Getting Started

is everything borked?

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.

general tools

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

the goal

  • 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

once per machine

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

once per project

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

Notes

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.

Read More

Extra Credit

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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment