Python pyenv and virtualenv usage note

Installation:


pyenv


Mac


brew update
brew install pyenv

To upgrade pyenv in the future, just use upgrade instead of install.

After installation, you’ll still need to add eval “$(pyenv init -)” to your profile (add it to ~/.bash_profile). You’ll only ever have to do this once.

#### Others

# Get Code
git clone https://github.com/yyuu/pyenv.git ~/.pyenv

# Set the Paths
echo ‘export PYENV_ROOT=”$HOME/.pyenv”‘ >> ~/.bash_profile
echo ‘export PATH=”$PYENV_ROOT/bin:$PATH”‘ >> ~/.bash_profile
Zsh note: Modify your ~/.zshenv file instead of ~/.bash_profile.
Ubuntu note: Modify your ~/.bashrc file instead of ~/.bash_profile.

# Initialize pyenv on load of terminal
echo ‘eval “$(pyenv init -)”‘ >> ~/.bash_profile
exec $SHELL

### pyenv-virtualenv

Mac


brew install pyenv-virtualenv

Add
eval “$(pyenv virtualenv-init -)”
to your profile (add it to ~/.bash_profile)

Others


# Install pyenv virtualenv
git clone https://github.com/yyuu/pyenv-virtualenv.git ~/.pyenv/plugins/pyenv-virtualenv

# auto ativate virtualenv for pyenv
echo ‘eval “$(pyenv virtualenv-init -)”‘ >> ~/.bash_profile
Zsh note: Modify your ~/.zshenv file instead of ~/.bash_profile.
Ubuntu note: Modify your ~/.bashrc file instead of ~/.bash_profile.
# restart shell
exec $SHELL

Usage


pyenv


# install python
pyenv install 2.7.9
pyenv install 3.4.3

if there are problems when install the python, you can refer to

https://github.com/yyuu/pyenv/wiki/Common-build-problems

rehash when install the new versions every time

pyenv rehash

set version of python to use for this session

pyenv shell 3.4.3

set global python for everytime we start pyenv

pyenv global 3.4.3

set python version for current and all sub directories

pyenv local 3.4.3

pyenv-virtualenv


Restart the shell and let’s create an virtualenv with Python 2.7.8.
pyenv virtualenv 2.7.8 project-a-2.7.8

Activate the newly created virtualenv.
pyenv activate project-a-2.7.8

You can deactivate the current virtualenv.
pyenv deactivate

And list the available virtualenvs.
pyenv virtualenvs

Those virtualenvs are located in ~/.pyenv/versions just like those installed Pythons. If you want to remove a virtualenv, simply delete that corresponding folder.
Or
pyenv uninstall my-virtual-env

Reference:
http://eureka.ykyuen.info/2014/07/11/pyenv-python-version-manager/
http://eureka.ykyuen.info/2014/07/14/manage-your-python-projects-with-virtualenv/
http://blog.froehlichundfrei.de/2014/11/30/my-transition-to-python3-and-pyenv-goodby-virtualenvwrapper.html
https://godjango.com/96-django-and-python-3-how-to-setup-pyenv-for-multiple-pythons/
https://github.com/yyuu/pyenv
https://github.com/yyuu/pyenv-virtualenv
http://www.openfoundry.org/tw/tech-column/8516-pythons-virtual-environment-and-multi-version-programming-tools-virtualenv-and-pythonbrew