python vtk教程,ImportError: No module named vtkCommonPython

可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):

问题:

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 "", line 1, in File "C:\vtk\Wrapping\Python\vtk\__init__.py", line 41, in 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:

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

回答3:

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

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!

回答4:

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.

回答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:

0444bbb00704e20ac11076b491287b99.png

回答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’

回答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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ModuleNotFoundError: No module named 'vtk'错误是由于Python环境中缺少名为'vtk'的模块导致的。这个错误通常是由于未正确安装或配置了vtk库所致。要解决这个问题,你可以尝试以下几个方法: 1. 确保你已经正确安装了vtk库。你可以使用pip命令来安装vtk,例如在命令行中运行"pip install vtk"。确保你使用的是正确的pip版本和Python环境。 2. 检查你的Python环境变量是否正确设置。确保你的Python环境可以找到正确的vtk库安装位置。你可以尝试在Python中运行"import vtk"来检查是否成功导入vtk库。 3. 如果你已经正确安装了vtk库但仍然出现这个错误,那么可能是因为vtk库的路径没有被正确添加到Python环境变量中。你可以尝试手动添加vtk库的路径到Python环境变量中,例如在Python脚本中添加以下代码: import sys sys.path.append('path/to/vtk/library') 这里的'path/to/vtk/library'应该替换为vtk库实际安装的路径。 综上所述,要解决"ModuleNotFoundError: No module named 'vtk'"错误,你可以尝试安装vtk库、检查Python环境变量设置,并确保vtk库的路径正确添加到Python环境变量中。希望这些方法可以帮助你解决问题。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [“No module namedvtk.util‘;‘vtk‘ is not a package”问题完美解决](https://blog.csdn.net/weixin_45656790/article/details/109697605)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *3* [【问题解决】No module named vtk](https://blog.csdn.net/Studywithme/article/details/123231022)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值