20

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?

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
user1842222
  • 201
  • 1
  • 2
  • 4
  • 5
    Do you have the vtkCommonPython.pyd file on your Python path? Is the file in one of the ``sys.path`` directories? – codeape Nov 21 '12 at 14:23

8 Answers8

4

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!

GnomeDePlume
  • 1,642
  • 2
  • 15
  • 31
4

I was having this same problem on MacOSX. So I started using vtk/bin/vtkpython instead of python. This allowed me to import vtk without any errors. I then imported vtkCommonCorePython explicitly and printed the location:

$ /home/vtk/bin/vtkpython
vtk version 6.2.0
Python 2.7.5 (default, Mar  9 2014, 22:15:05) 
Type "help", "copyright", "credits" or "license" for more information.
>>> import vtkCommonCorePython 
>>> print vtkCommonCorePython
<module 'vtkCommonCorePython' from '/home/vtk/lib/vtkCommonCorePython.so'>

In the terminal, I then added the lib folder to my python virtual environment path:

$ add2virtualenv /home/vtk/lib

I don't know how this would translate to Windows, but I hope this helps!

David E.
  • 133
  • 7
2

I was having the same problem (kubuntu 14.04).

I realized that the links to the compiled vtkpython objects in /usr/lib/python2.7/dist-packages/vtk were broken.

So I went to /usr/lib/x86_64-linux-gnu and ran for i in *-6.0.so.6.0; do sudo ln -s $i ${i/-6.0.so.6.0/-6.0.so}; done.

It seemed to solve the issue.

Hope that helps.

Martin
  • 361
  • 4
  • 12
2

Apart from checking that vtk is inside your $PYTHONPATH, also note that more recent versions of VTK (6.x) no longer have a vtkCommonPython module. Instead it has been split into several sub-components. (e.g. vtkCommonCorePython, vtkCommonMathPython, vtkCommonSystemPython), but this will mostly be a problem for external packages you might want to use (e.g. VMTK is by default compiled against and dependend upon VTK 5.10.

To check and expand your $PYTHONPATH simply call:

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

To check which vtk version you have installed:

$ python
Python 2.7.10
>>> import vtk
>>> print str(vtk.VTK_MAJOR_VERSION) + '.' + str(vtk.VTK_MINOR_VERSION)
6.2
Chris
  • 3,245
  • 4
  • 29
  • 53
  • The problem is that "import vtk" generates an error. Adjusting the path does not solve this problem. "import vtk" still generates an error. – ToddP May 28 '19 at 01:43
  • Actually `from vtkCommonPython import *` generates the error, which can not be found (`No module named vtkCommonPython`). Hence, you need to make sure that all `vtk` modules are in the `PYTHONPATH`. – Chris Jun 17 '19 at 07:46
  • Also, this is an issue from 2012 originally and I am not sure if VTK 8 still has the same module layout. – Chris Jun 17 '19 at 07:48
  • Lastly, could you please explain why all of the (upvoted) answers (including mine) here are proposing a similar solution (adjusting the python environment setup), yet you downvote this one? – Chris Jun 17 '19 at 07:51
1

It needs to be in the directory of the sys.path. What I did is I created a folder (doesn't really matter where) called "Modules" in which I have all of my modules that I download/create in there. Say I put it in C:\Users\USER\Modules. You can put this module in there as well.

You need to copy the path to the folder.

Then, go to Control Panel. Click System, then on the left panel there is an option called "Advanced System Settings". Click that. From the bottom of the window that pops up, click "Environment Variables". Look to see if you have a variable created called PYTHONPATH. Most likely, you don't. So, create a variable (in the second section) by pressing "NEW". Name it PYTHONPATH and for the Variable value, put in the file path. (For my example, the file path is C:\Users\USER\Modules). Hope this helps :)

I inserted a screenshot of how to get there once you get to the System (Properties) location in Control Panel: Python Path

Rushy Panchal
  • 16,979
  • 16
  • 61
  • 94
0

You need to add the folder of vtkCommonPython.pyd also to the system path (like if it was a dll). See also http://www.vtk.org/Wiki/VTK/Tutorials/PythonEnvironmentSetup

lib
  • 2,918
  • 3
  • 27
  • 53
  • I have the same error: No module named 'vtk.vtkRenderingQtPython'. My computer is win7. I can find vtkRenderingQtPython.pyd in vtk/bin/Debug. And I have add the folder to the system environment 'PYTHONPATH'. But the error still existed. How can I fix this error? – Qiang Zhang Jan 29 '18 at 12:44
0

Assuming you used the suggested paths in the instructions, add the following line to your bashrc:

export PYTHONPATH=$HOME/projects/VTK-build/lib:$HOME/projects/VTK-build/Wrapping/Python:$PYTHONPATH

If the problem occurs when using sudo, e.g.

sudo yum install mayavi

you probably need to resolve this issue. Add the following line to the bashrc:

alias sudo=’sudo env PYTHONPATH=$PYTHONPATH’

Full instructions

Community
  • 1
  • 1
Lila
  • 509
  • 4
  • 5
0

In windows,

You are supposed to add the following path
Add the folders containing the .pyd and .dll files to the PYTHONPATH environment variable.

Example:

D:\VTK\VTK-bin\bin\Release\Lib\site-packages\vtkmodules
D:\VTK\VTK-bin\bin\Release

Additionally, add the path to bin folder of Qt to the System variable PATH C:\Qt\5.10.0\msvc2017_64\bin