Skip to content

Instantly share code, notes, and snippets.

@lucsantos1975
Last active May 22, 2019 00:43
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lucsantos1975/4833ec180e8683d19413d03713616a66 to your computer and use it in GitHub Desktop.
Save lucsantos1975/4833ec180e8683d19413d03713616a66 to your computer and use it in GitHub Desktop.
Python development using pyenv virtual environments

Instalation

This step-by-step was based on Henrique Bastos' article: https://medium.com/@henriquebastos/guia-definitivo-para-organizar-meu-ambiente-python-a16e2479b753

1 - Clone pyenv and plugins repositories:

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

2 - Create separate directories for your virtual environments and projetcs:

mkdir ~/.ve
mkdir ~/Workspace

3 - Define pyenv environment variables and some new configurations at the end of the .bashrc file:

export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"

export WORKON_HOME=~/.ve
export PROJECT_HOME=~/Workspace
eval "$(pyenv init -)"

4 - Update your shell with the new configuration:

source ~/.bashrc

5 - Install some system packages:

sudo apt-get install -y make build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev libncursesw5-dev xz-utils

6 - Install Python 2.7.13 and 3.6.0 (or newer) versions into pyenv:

pyenv install 2.7.13
pyenv install 3.6.0

7 - Global instalation of Jupyter Notebook and iPython for Python 3:

pyenv virtualenv 3.6.0 jupyter3

8 - Global instalation of iPython Console for Python 2:

pyenv virtualenv 2.7.13 ipython2

9 - Jupyter kernel configuration for Python 3:

pyenv activate jupyter3
pip install jupyter
python -m ipykernel install --user
pyenv deactivate

10 - Jupyter kernel configuration for Python 2:

pyenv activate ipython2
pip install ipykernel
python -m ipykernel install --user
pyenv deactivate

11 - Path priority configuration:

pyenv global 3.6.0 2.7.13 jupyter3 ipython2

12 - Add one more configuration at the end of the .bashrc file:

pyenv virtualenvwrapper_lazy 

13 - Update the shell again:

source ~/.bashrc

How to create and use virtualenvs for your projects

1 - Start a new project named "prj3" with Python 3 (default):

mkproject prj3

2 - If you stop working on prj3, deactivate:

deactivate

3 - To work again on prj3:

workon prj3

4 - Create a Python 2 virtualenv for an existing "prj2" project on your computer (cloned from Github, for exemple) :

mkvirtualenv -a ~/Workspace/prj2 -p python2 prj2

How to use Jupyter and iPython

1 - Create iPython default profile and install initialization script created by Henrique Bastos:

ipython profile create
curl -L http://hbn.link/hb-ipython-startup-script > ~/.ipython/profile_default/startup/00-venv-sitepackages.py

2 - Working on a project, execute:

jupyter notebook

or

ipython
@alexander-chan
Copy link

Installing Python < 3.5 on Pyenv had OpenSSL Errors on Ubuntu 18
Fixed via

sudo apt-get remove libssl-dev
sudo apt-get update
sudo apt-get install libssl1.0-dev

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment