问题
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?
回答1:
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!
回答2:
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!
回答3:
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.
回答4:
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
回答5:
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:

回答6:
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
回答7:
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
回答8:
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
来源:https://stackoverflow.com/questions/13495285/importerror-no-module-named-vtkcommonpython