Anaconda vs {pyenv + pipenv}

Updated January 2021 · 5 minute read
The following assume Ubuntu 16.04 with bash shell, as some tools and scripts do not apply to other OS.

Anaconda/Miniconda

Anaconda is a cross-platform software distribution from Continuum Analytics Inc., providing mainly a fast and easy way to do Python and R data science and machine learning, meaning that it is a collection of packages that can be installed and used on a system (+700 packages including the so called PyData ecosystem). Anaconda packages include Conda, an open source package, dependency and environment management system that runs on Windows, macOS and Linux for any language —Python, R, Ruby, Lua, Scala, Java, JavaScript, C/ C++, FORTRAN.

Conda is a part of the Anaconda Distribution. It is installed by most people through the installation of Anaconda or miniconda, a minimal installer for conda that includes conda and its dependencies (Python, the packages they depend on and a small number of other useful packages, including pip, zlib and a few others). Conda can also be installed independently of Anaconda/miniconda, in a system that has Python and pip installed using:

pip install conda

However, the above method will not give Conda as a standalone application, as the currently supported install methods include the Anaconda installer and the miniconda installer. To install miniconda download the appropriate installer from here, or for a x64 system and Python 3 at the cli:

$ wget https://repo.continuum.io/miniconda/Miniconda3-latest-Linux-x86_64.sh
$ bash Miniconda3-latest-Linux-x86_64.sh

Installing Anaconda means you will be using a minimum of 3 Gb of your disk space while installing miniconda means you will be using around 400 Mb. Also, you will have to adapt to the work-flow required by the tools and conda-environments you will be using. Of course adapting to a certain work-flow will happen with whatever tools, environments, etc. one might use.

Installing Anaconda is quite trivial and one can start developing, researching, learning almost immediately after installation. Depending to what extend one might use Anaconda (or miniconda) for, the above setup might be ideal or even redundant.

Pyenv, pipenv, Jupyter Notebook, JupyterLab

For someone with some experience with command line and python, a similar setup can be achieved with a little more effort, allowing you to also gain valuable knowledge in the process. Based on the guide written by Henrique Bastos, we are going to:

  1. install pyenv - a simple Python version management
  2. create some directories and some global variables
  3. Install some Python versions
  4. create virtual environments with these Python versions
  5. Install pipenv - a tool that aims to bring the best of all packaging worlds (bundler, composer, npm, cargo, yarn, etc.) to the Python world.
  6. Install Jupyter Notebook and JupyterLab
  7. Incorporate all the above in a single script to run things faster.

The script will create two directories, one for virtual environments, and one to use as a workspace for projects:

sudo apt update
# Install required
sudo apt -y install unzip wget curl build-essential
sudo apt -y install python-dev python3-dev
# install pyenv
curl -L https://github.com/pyenv/pyenv-installer/raw/master/bin/pyenv-installer | bash
source ~/.bashrc
git clone https://github.com/pyenv/pyenv-virtualenvwrapper.git $(pyenv root)/plugins/pyenv-virtualenvwrapper
mkdir -p $HOME/.envs
mkdir -p $HOME/workspace
echo 'export WORKON_HOME=~/.envs' >> ~/.bashrc
echo 'export PROJECT_HOME=~/workspace' >> ~/.bashrc
source ~/.bashrc
# Install python versions 3.8 and 2.7 and create virtual environments for jupyter to work globally.
pyenv install 3.8.7
pyenv install 2.7.18
pyenv virtualenv 3.8.7 jupyter3
pyenv virtualenv 2.7.18 ipython2
pyenv activate jupyter3
pip install --upgrade pip
pip install jupyter
pip install jupyterlab
python -m ipykernel install --user
pip install ipywidgets
jupyter nbextension enable --py widgetsnbextension --sys-prefix
pyenv deactivate
pyenv activate ipython2
pip install --upgrade pip
pip install ipykernel
python -m ipykernel install --user
pyenv deactivate
# Set system wide python versions
pyenv global 3.8.7 2.7.18 jupyter3 ipython2
pip install --upgrade pip
pip install pipenv
echo 'pyenv virtualenvwrapper_lazy' >> ~/.bashrc
echo '# For pipenv' >> ~/.bashrc
echo 'eval "$(pipenv --completion)"' >> ~/.bashrc
source ~/.bashrc
ipython profile create
curl -L http://hbn.link/hb-ipython-startup-script > ~/.ipython/profile_default/startup/00-venv-sitepackages.py

To run the script:

  1. Create a .sh file in your home (~) directory, eg. pyenvscript.sh.
  2. Copy the above script and paste in the file.
  3. Run the script in a terminal:
bash ./pyenvscript.sh

You should now be able to:

Example usage would be:

cd ~/workspace
mkdir myproject && cd myproject
# create a new project with python 3.9.
# If pyenv is installed pipenv will automatically download and install python 3.9
pipenv --python 3.9
pipenv run which python
pipenv shell
python --version
pipenv install numpy
pipenv install scikit-learn
exit

Similarly to miniconda the above setup would provide a development environment that could be used to develop Python projects, install learning tools, like fast.ai, etc. With pyenv-virtualenv you could even manage conda environments by “conda create” as same manner as standard Anaconda/Miniconda installations. At about 400 Mb you have two Python versions installed with pip and tools (Jupyter NB, JupyterLab, pipenv). You would be required to install various dependencies according to your project, unlike Anaconda where almost everything is installed by default, but that is in the procedure of learning and developing.

So unless you are a pure data-scientist dealing only with the PyData-ecosystem and looking to build a robust python environment for development, learning or researching , maybe Anaconda and Miniconda is not the only way to go.

Enjoy coding!

References for further reading:

  1. pyenv commands reference
  2. pyenv-virtualenv
  3. pyenv virtualenvwrapper
  4. pipenv documentation
Above opinions and any mistakes are my own. I am not affiliated in any way with companies, or organizations mentioned above. The code samples provided are licensed under the Apache 2.0 License and rest content of this page is licensed under the Creative Commons Attribution 3.0 License, except if noted otherwise.