ImportError: No module named vtkCommonPython

user1842222 picture user1842222 · Nov 21, 2012 · Viewed 22.4k times · Source

I am trying to install Python with VTK on my computer, but when I want to import VTK, I get an error:

import vtk 
Traceback (most recent call last): 
 File "<stdin>", line 1, in <module>
 File "C:\vtk\Wrapping\Python\vtk\__init__.py", line 41, in <module>
   from vtkCommonPython import * 
ImportError: No module named vtkCommonPython 

I already checked my paths and I have the file 'vtkCommonPython.pyd' in the bin folder.

Can anyone help me with this problem?

Answer

GnomeDePlume picture GnomeDePlume · Jun 25, 2014

I ran into a very similar problem, and fixed it by adding /usr/local/lib/python2.7/site-packages/vtk/ to the PYTHONPATH environment variable.

Your exact fix may vary depending on your version of python, etc (it affects the paths). You should be able to sort it out by locating the location of the missing module and then adding the path to the environment variable as follows.

In my case, I found the path using:

find / -name vtkCommonCorePython 2>/dev/null

And then added the relevant path to ~/.bash_rc or equivalent.

export PYTHONPATH="$PYTHONPATH:usr/local/lib/python2.7/site-packages/vtk/"

Be careful that you append to the path variable rather than overwriting it - you probably already need to have some other stuff like '/usr/local/lib/' in there. The format (and file where you put this!) is different for different shells.

Restart the terminal to get the changes through, and then check that the variable is set up correctly:

echo $PYTHONPATH

And be very careful that there are no mistakes in any of the paths!