Manage Python Develop Environment with Pyenv and Vitualenv

What are Pyenv and Vitualenv?

pyenv is a simple python version management tool. pyenv lets you easily switch between multiple versions of Python.

Vitualenv is a tool to create isolated Python environments.

Install Pyenv

1. Install curl and git

sudo apt-get install curl git-core
2. Install pyenv
curl -L https://raw.github.com/yyuu/pyenv-installer/master/bin/pyenv-installer | bash
pyenv will be installed in ~/.pyenv
3. Add codes to ~/.bashrc
export PYENV_ROOT="${HOME}/.pyenv"

if [ -d "${PYENV_ROOT}" ]; then
  export PATH="${PYENV_ROOT}/bin:${PATH}"
  eval "$(pyenv init -)"
fi
Above codes set pyenv location, so terminal can execute pyenv commands.
4. Make .bashrc effect
source ~/.bashrc

Install Python with Pyenv

1. check available versions of python
pyenv install –list
2. Install python 2 and python 3
pyenv install 2.7.11
pyenv install 3.4.4
3. Check installed python
pyenv versions
Output:
* system (set by /home/jack/.pyenv/version)
  2.7.11
  3.4.4

Switch python versions

pyenv global 3.4.4

pyenv global system

Uninstall python

pyenv uninstall x.x.x

Create Virtual Python Environment with virtualenv

Pyenv has installed the vitrualenv plugin, so we need not install vitrualenv again.

1. Create a vitrual environment of python 3

pyenv virtualenv 3.4.4 env3
Above command creates a python virtual environment named env3. env3 is located at ~/.pyenv/versions/.
Command ‘pyenv versions’ could display current virtual environment:
* system (set by /home/jack/.pyenv/version)
  2.7.11
  3.4.4
env3

Switch Virtual Environment

Switch to env3

pyenv activate env3
Now you can do anything in env3 and won’t effect system and other virtual environments.
Switch back to system environment
pyenv deactivate
Delete a virtual environment
rm -rf ~/.pyenv/versions/env3/

References

  • pyenv github
  • Virtualenv
  • 用pyenv 和 virtualenv 搭建单机多版本python 虚拟开发环境

To Be Read

  • 我们在360如何使用Python – virtualenv 篇
  • Buildout Tutorial

你可能感兴趣的:(Manage Python Develop Environment with Pyenv and Vitualenv)