Two different environments

Creating New Environment With Conda

Two different environments

Creating New Environment With Conda

One of the frustrating things bioinformaticians encounter routinely is the incompatibities among packages and their dependencies when they upgrade a package. An update is recommended because it usually offers more efficient, elegant and secure functionalities to the users. However, this comes at a price. An update may break an existing workflow or pipelines installed on the system. Figuring out what went wrong and what other dependencies need to be upgraded can be a time consuming task. Check this blog out to read more about this issue.

Today I was working with networkx API and wanted to draw CircosPlot of a sample of GitHub user collaboration data . I had networkx, nxviz, matplotlib installed in my current shell environment so plotting should have been eventless.

import networkx as nx
import matplotlib.pyplot as plt
import nxviz as nv

G = nx.read_gpickle('github.p')

Before moving further I decided to check if everything was ok…

G.nodes()
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-3-083f395b092f> in <module>
----> 1 G.nodes()
.
.
.
AttributeError: 'Graph' object has no attribute '_node'

The problem here is the pickle file github.p which was created using networkx version 1.11 while I had version 2.2 installed.

Checking the list of installed packages and their versions

pip list

Obvious solution to the problem here is to downgrade networkx package to version 1.11. But this may break other piece of code which works fine on this machine.

This is a perfect scenario for using virtual environment. A virtual environment is a named copy of python complete with its own environment variables, packages and dependencies which is isolated from other environments on the system. You can create a new environment and load it with desired versions of the packages and their dependencies. The packages in other environments will remain installed but will not be used. A new virtual environment is created using conda command. You need to be running the anaconda distribution of python.

Creating a new virtual environment

$ conda create -n networkx_downgrade python=3.7 anaconda

This creates a new conda environment with a name ‘networkx_downgrade’.

Loading the new environments

$ conda activate networkx_downgrade

This will load the newly created virtual environment. Now you can uninstall and install the desired versions of packages and their dependencies

$ pip uninstall networkx
$ pip install networkx==1.11
$ pip uninstall nxviz
$ pip install nxviz==0.3.1

At this point you can also check the versions of packages on which your just downgraded package depended, and update them accordingly.

The original installed versions of the programs are safe in other environments that can be loaded if you need them.

To deactivate an active environment, do following

$ conda deactivate

Checking if my problem is solved

import networkx as nx
import matplotlib.pyplot as plt
import nxviz as nv

G = nx.read_gpickle('github.p')

G.nodes()
['u10002',
 'u21097',
 'u24070',
 'u31908',
 .
 .
 .

Perfect!