Package Management with Conda

When you start working with Python, you begin with a core set of Python code. As you branch out and start incorporating more functionality in your code, you will need to import packages using statements like this:

import numpy as np
import datetime as dt
import os
import pandas import netcdf4

Each of these commands imports the library specified so that you can access the various functions and objects in the library. In order for this work, you have to install the libraries on your computer, otherwise you will get an "unrecognized" error when you try to run your code. Furthermore, some libraries (or blocks of code you download from the internet) are dependent on other libraries. To make things worse, each library has a version number. And a library can be dependent on a specific version of another library.

PIP

Keeping all of this straight on your computer can appear to be daunting. However, it can be automated using package managers and virtual environments. A package manager provides a simple way to download whatever library you need and to automatically update and satisfy dependencies. Historically, package management in Python has been accomplished with a utility called PIP (Pip Installs Packages). PIP accesses the Python Package Index (PyPI) which is a repository of Python software packages. After installing PIP, you can download and install a library simply by typing:

$ pip install jupyter

To get the latest version of a package:

$ pip update jupyter

Conda

PIP is great, but a few years ago some folks got together to develop a new package management system called Conda. Conda supports Python and does basically the same thing as PIP, but it can be used for anything, not just Python. For example, starting with version 2.0, Tethys installation is now done with Conda and it is far simpler and more automated than before. Here is a link that discuss the differences between PIP and Conda and describes the history of Conda.

https://jakevdp.github.io/blog/2016/08/25/conda-myths-and-misconceptions/

There are two ways to install and use Conda: Anaconda and Miniconda. Both include the main Conda package manager. Anaconda includes just about every library you could expect to use plus some additional features (IDE's, etc.). Miniconda starts with just the basics and lets you install only what you need. Differences explained here:

http://getoptimise.com/blog/tools/anaconda-vs-miniconda/

We recommend you use Miniconda.

Installing Miniconda

To install Miniconda, go to the following link and download the appropriate version for the OS you are using:

https://conda.io/miniconda.html

We recommend that you install the Python 3.x version of Miniconda. If you install the 2.7 version, you will get a folder titled "miniconda". If you install the 3.x version, you will get a folder titled "miniconda3". With either one, you can create a virtual environment for any version of Python. The only difference is that with the 3.x version, your virtual environments will default to Python 3.x unless you specify otherwise.

For the Windows version:

1. Download the *.exe install file

2. Run the *.exe installer and follow the instructions.

3. When you get to this window, select the options shown. The top selection is necessary to ensure that Conda can be used with the instructions listed below for this exercise.

4. If you have a CMD window open, close it and open a new one.

For Mac OS X and Linux versions:

1. Copy the install script you downloaded to your home folder.

2. Open a terminal window and type:

$ bash <scriptfile name>

For example:

$ bash Miniconda3-latest-MacOSX-x86_64.sh

3. Type "yes" to the options when prompted.

4. When finished, kill the terminal window and launch a new one. This will launch Conda.

For both Windows and Mac OS X, test the Conda install by typing:

$ conda list

This should display a list of the installed packages in your root installation of Conda.

You will also see a new folder in your home directory titled “miniconda3”.

Conda-Forge Channel

Before we start installing packages, we need to add the conda-forge channel. Many of the spatial programming packages we need are hosted on conda-forge, so we will add it to the list of channels referenced by Conda. Type the following:

$ conda config --add channels conda-forge

Virtual Environments

In addition to managing packages, Conda also makes it very easy to set up Python virtual environments. A virtual environment is like a sandbox - it allows you to create an isolated environment to set up a particular version of Python with a particular suite of packages. By installing Conda, we just set up the “root” conda environment. In theory, we could immediately start downloading/installing packages and developing python code. However, it is best practice to set up other virtual environments and do the installations and coding there. A virtual environment is like a sandbox where you can set up a specific combination of packages with different versions for each package and this environment is isolated from the other environments. For example, you may have some python code that requires python 2.7 and other code that requires python 3.5. You can create an environment for each and get all the dependencies that you need installed and then just switch back and forth between virtual environments. Tethys uses a virtual environment to ensure that it has the python dependencies that it needs and to ensure you don’t mess it up when you install something outside of that environment. For more information on environments:

https://conda.io/docs/using/envs.html#create-an-environment

Setting up a Python 2.7 Environment

Create a python 2.7 environment by typing the following

$ conda create -n py2k python=2.7

To activate the environment on Windows:

$ activate py2k

To activate the environment on Linux or Mac OS X:

$ source activate py2k

Note new text to the left of the command prompt. This indicates that the environment is active.

To deactivate on Windows:

(py2k) $ deactivate

To deactivate on Linux or Mac OS X:

(py2k) $ source deactivate

Setting up a Python 3.x Environment

Create a python 3.x environment by typing the following

$ conda create -n py3k python=3

Actually, we could omit the “python=3” part because we are using miniconda3 and it will default to python3 unless we tell it otherwise. We can also designate a specific flavor of python3 (3.4, 3.5, 3.6). If we just use “3” it defaults to the latest.

To activate the environment on Windows:

$ activate py3k

To activate the environment on Linux or Mac OS X:

$ source activate py3k

As was the case above, you should see some new text to the left of the command prompt. This indicates that the environment is active.

Once again, to deactivate on Windows:

(py3k) $ deactivate

To deactivate on Linux or Mac OS X:

(py3k) $ source deactivate

Dynamically Switching Python Versions

In some cases you may wish to switch Python versions within an environment. You can do very easily using the instructions shown here:

http://chris35wills.github.io/conda_python_version/

BASH Shortcuts

If you are using Mac OS X or Linux, you can save yourself some typing by creating some shortcuts. Path to your home folder and open the hidden bash_profile file in the vi text editor as follows:

$ vi .bash_profile

If you aren't familiar with the vi editor, here is a resource:

http://www.lagmonster.org/docs/vi.html

Or you can open the file with any text editor that allows you to open a hidden file (TextWrangler for example).

Once you have opened the file in the text editor, add the following lines the to the file:

#Shortcuts for launching and closing python environments
alias py2k="source activate py2k"
alias py3k="source activate py3k"
alias nopy="source deactivate"

Save your changes and kill the terminal and reopen it to make it active. Then you can just type:

$ py3k

to launch the py3k virtual environment. And you can type:

$ nopy

to exit either environment.

Installing Python Packages

Now we are ready to install some Python packages. First of all, activate the py3k environment, if necessary.

For Windows:

$ activate py3k

For Linux or Mac OS X:

$ source activate py3k

Then install the following packages, as we will need them for some of the following exercises. Hit enter to confirm in each case.

$ conda install numpy
$ conda install matplotlib
$ conda install pandas
$ conda install jupyter
$ conda install pyshp
$ conda install pyproj
$ conda install netcdf4

When you are finished, type the following to view the list of packages installed in this environment.

$ conda list

Remember, always do your installations in a virtual environment and keep your root environment pristine.

 

 

BYU Hydroinformatics Group

Brigham Young University
Dept. of Civil and Environmental Engineering