2016-03-11 08:19:40 -08:00
+++
2020-09-06 22:07:03 -07:00
title = "Using pyenv for Python projects"
2024-02-10 18:04:44 -08:00
description = "Using pyenv to manage your virtual environments makes working on multiple projects, each using a different version of python a breeze."
2016-03-11 08:19:40 -08:00
date = "2015-04-13T09:06:03-05:00"
2024-02-10 18:04:44 -08:00
[taxonomies]
tags = ["python"]
categories = ["Developer Tools"]
[extra]
2020-09-06 22:07:03 -07:00
toc = true
2024-02-10 18:04:44 -08:00
keywords = ["python", "pyenv"]
2016-03-11 08:19:40 -08:00
+++
Using [pyenv][3] to manage your virtual environments makes working on multiple projects, each using a different version of python a breeze.
I do all my development on an Apple Macbook running Yosemite and my production environment is a VPS from [Linode][1] running [CentOS 7][2].
Here some simple notes on how I setup and use [pyenv][3] :
## Installing on Mac OS X ##
2018-02-18 00:25:45 -08:00
**Install using [homebrew][5]**
2016-03-11 08:19:40 -08:00
2024-02-10 18:04:44 -08:00
```bash
brew install pyenv pyenv-virtualenv
2016-03-11 08:19:40 -08:00
```
2017-03-03 13:23:47 -08:00
**Update your shell profile (.bashrc or .zshrc) adding the following to it (and restart your terminal)**
2016-03-11 08:19:40 -08:00
2024-02-10 18:04:44 -08:00
```bash
2016-03-11 08:19:40 -08:00
if which pyenv > /dev/null; then eval "$(pyenv init -)"; fi
if which pyenv-virtualenv-init > /dev/null; then eval "$(pyenv virtualenv-init -)"; fi
```
## Installing on Linux CentOS 7 ##
2017-03-03 10:41:45 -08:00
**Checkout from github**
2016-03-11 08:19:40 -08:00
2024-02-10 18:04:44 -08:00
```bash
git clone https://github.com/yyuu/pyenv.git ~/.pyenv
git clone https://github.com/yyuu/pyenv-virtualenv.git ~/.pyenv/plugins/pyenv-virtualenv
2016-03-11 08:19:40 -08:00
```
2017-03-03 10:41:45 -08:00
**Update your shell profile (.bashrc or .zshrc) adding the following to it (and restart your terminal)**
2016-03-11 08:19:40 -08:00
2024-02-10 18:04:44 -08:00
```bash
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
2016-03-11 08:19:40 -08:00
```
## Using pyenv ##
2017-03-03 10:41:45 -08:00
**To install a new version of [Python][6]**
2016-03-11 08:19:40 -08:00
2024-02-10 18:04:44 -08:00
```bash
pyenv install < version >
pyenv rehash
2016-03-11 08:19:40 -08:00
```
2017-03-03 10:41:45 -08:00
**To get a list of [Python][6] versions available**
2016-03-11 08:19:40 -08:00
2024-02-10 18:04:44 -08:00
```bash
pyenv install -l
2016-03-11 08:19:40 -08:00
```
2017-03-03 10:41:45 -08:00
**To create a new virtual environment**
2016-03-11 08:19:40 -08:00
2024-02-10 18:04:44 -08:00
```bash
pyenv virtualenv [pyenv-version] [virtualenv-name]
2016-03-11 08:19:40 -08:00
```
2017-03-03 10:41:45 -08:00
**To use your new virtual environment within your project**
2016-03-11 08:19:40 -08:00
1. Change to your projects root directory
1. Run:
2024-02-10 18:04:44 -08:00
```bash
pyenv local [virtualenv-name]
2016-03-11 08:19:40 -08:00
```
Note that this is done only the first time you go to your project directory. The wonderful thing about [pyenv][3] is in future when you change directory to your project directory, it will be automatically activated your virtualenv for you.
[1]: https://www.linode.com
[2]: https://www.centos.org
[3]: https://github.com/yyuu/pyenv
[4]: https://github.com/yyuu/pyenv-virtualenv
[5]: http://brew.sh
[6]: https://www.python.org