3

I am trying to follow this post, but it fails to display a PDB inside a Jupyter notebook:

import MDAnalysis as mda
import nglview as nv
from nglview.datafiles import PDB, XTC

u = mda.Universe(PDB, XTC)

protein = u.select_atoms('protein')

When I try to do:

w = nv.show_mdanalysis(protein)
w

I get:

---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
<ipython-input-2-499e28f0ffd3> in <module>()
----> 1 w = nv.show_mdanalysis(protein)
      2 w

~/anaconda3/lib/python3.6/site-packages/nglview-0.4-py3.5.egg/nglview/__init__.py in show_mdanalysis(atomgroup, **kwargs)
    118     '''
    119     structure_trajectory = MDAnalysisTrajectory(atomgroup)
--> 120     return NGLWidget(structure_trajectory, **kwargs)
    121 
    122 

~/anaconda3/lib/python3.6/site-packages/nglview-0.4-py3.5.egg/nglview/__init__.py in __init__(self, structure, trajectory, representations, parameters, **kwargs)
    347         if parameters:
    348             self.parameters = parameters
--> 349         self.set_structure(structure)
    350         if trajectory:
    351             self.trajectory = trajectory

~/anaconda3/lib/python3.6/site-packages/nglview-0.4-py3.5.egg/nglview/__init__.py in set_structure(self, structure)
    372     def set_structure(self, structure):
    373         self.structure = {
--> 374             "data": structure.get_structure_string(),
    375             "ext": structure.ext,
    376             "params": structure.params

~/anaconda3/lib/python3.6/site-packages/nglview-0.4-py3.5.egg/nglview/__init__.py in get_structure_string(self)
    313                 "'MDAnalysisTrajectory' requires the 'MDAnalysis' package"
    314             )
--> 315         import cStringIO
    316         u = self.atomgroup.universe
    317         u.trajectory[0]

ModuleNotFoundError: No module named 'cStringIO'

Edit Feb. 2020: I believe this is not an issue anymore if the libraries you are using updated their code to python3 properly: python 3.x ImportError: No module named 'cStringIO', and it should be ok by now as python2 is deprecated.

0x90
  • 39,472
  • 36
  • 165
  • 245
  • nglview 0.4 seems terrible out of date, the latest on https://github.com/arose/nglview/releases is 1.1.4. (Your problem looks like code that was not properly configured to run under Python 2 *and* Python 3.) – orbeckst May 18 '18 at 17:21
  • @orbeckst I run under python 3.x. But that's what Fonda install – 0x90 May 18 '18 at 17:22
  • In a conda environment with Python 3.6 I did a `conda install nglview` and it pulled *1.1.3-py36_0 conda-forge* (I use conda-forge channel) with jupyter 4.4.0. The example from https://www.mdanalysis.org/2016/03/14/nglview/ worked as advertised. – orbeckst May 18 '18 at 17:25
  • *I run under python 3.x. But that's what Fonda install*. Maybe the default conda channel carries outdated nglview packages? – orbeckst May 18 '18 at 17:27
  • 1
    Aren't you using conda? At least that what it looks like from your stack trace. Can you try uninstalling nglview with `conda remove nglview`? If this fails then this package might have been pip or easy_installed. Try `pip remove nglview`. If this still not helping, `rm -r` everything related to nglview manually. Then `conda install -c conda-forge nglview`. (I tried conda installation under Ubuntu 16.04.4 and Python 3.6.4 and it worked without problems.) – orbeckst May 19 '18 at 00:14

1 Answers1

2

I would attempt to get a clean nglview installation via conda (and the Anaconda distribution):

Try uninstalling nglview with

conda remove nglview

just in case there's an old version. If this conda-remove fails then this package might have been pip or easy_installed. Try pip remove nglview. If this still not helping, you have to look for the installed nglview package and manually remove it with rm but I do not want to put copy&paste instructions here as this requires careful looking at files.

Once all traces of nglview have been removed, install the latest version of nglview from the conda-forge channel with conda:

conda install -c conda-forge nglview
orbeckst
  • 3,189
  • 1
  • 17
  • 14