September 18, 2014

Blast into PyEnv

I've discovered two nice things recently: pyenv and pyenv-virtualenv. Here I'd like to tell how to install and start to use them in a couple of minutes.

0
Prepare to take-off.

sudo apt-get install -y make build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev libncurses-dev libsqlite3-dev wget curl llvm git


1
Go to your home dir and clone pyenv and pyenv-virtualenv (you may clone them to whatever you like, just pay attention for the PYENV_ROOT in the next step).

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


2
Update your env.

echo '' >> ~/.bash_profile
echo '### PyEnv' >> ~/.bash_profile
echo 'export PYENV_ROOT="$HOME/.pyenv"' >> ~/.bash_profile
echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bash_profile
echo 'eval "$(pyenv init -)"' >> ~/.bash_profile
echo 'eval "$(pyenv virtualenv-init -)"' >> ~/.bash_profile


Use .zshrc instead of .bash_profile for ZSH.


3
Apply changes.

exec $SHELL


4
Install some versions of Python. They will be compiled, so take a look at common build problems if you encounter any.

pyenv install 2.6.9
pyenv install 2.7.8
pyenv install 3.4.1
pyenv rehash


5
Voila! You've got 4 different Pythons!

pyenv versions
* system (set by /home/alex/.pyenv/version)
  2.6.9
  2.7.8
  3.4.1


6
Let's play with virtualenv now and create one for Python 2.6.9.

mkdir sandbox && cd sandbox
pyenv virtualenv 2.6.9 sandbox-2.6.9
pyenv activate sandbox-2.6.9


7
Ensure it's OK.

pyenv versions              
  system
  2.6.9
  2.7.8
  3.4.1
* sandbox-2.6.9 (set by PYENV_VERSION environment variable)


8
Let's pull something from PyPI.

pip install bpython
which bpython 
/home/alex/.pyenv/versions/sandbox-2.6.9/bin/bpython


9
Launch the rocket.

bpython
>>> import sys
>>> print sys.version
2.6.9 (unknown, Sep 18 2014, 14:57:31) 
[GCC 4.8.2]


10
Yay! Enjoy environment virtualization!

No comments:

Post a Comment