You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

M2 MacBook Air运行地磁场模拟Python代码遇VTK缺失错误

解决M2 MacBook Air上PyVista导入时的VTK缺失问题

问题重现

运行以下模拟地磁场的Python代码:

import numpy as np
import matplotlib.pyplot as plt
import magpylib as magpy
import pyvista as pv

ts = np.linspace(-8,8, 150)
t = np.linspace(-6,6, 150)
axis = np.c_[2*np.cos(ts*2*np.pi), 2*np.sin(ts*2*np.pi), ts]
aux = np.c_[2*np.cos(ts*2*np.pi), 2*np.sin(ts*2*np.pi), t]
def make_coil(pos, vertices):
    coil = magpy.current.Line(
    current = 100,
    vertices = vertices,
    position= pos,
    style_color="green",
    )
    return coil

theta = np.sqrt(2)/2
r = 4
coil1 = make_coil((0,0,0), axis)
coil2 = make_coil((r*1,0,0), aux)
coil3 = make_coil((r*theta,r*theta,0), aux)
coil4 = make_coil((0,1*r,0), aux)
coil5 = make_coil((-r*theta,r*theta,0), aux)
coil6 = make_coil((-r*1,0,0), aux)
coil7 = make_coil((-r*theta,-r*theta,0), aux)
coil8 = make_coil((0,-r*1,0), aux)
coil9 = make_coil((r*theta,-r*theta,0), aux)

coil = coil1 + coil2 + coil3 + coil4 + coil5 + coil6 + coil7 + coil8 + coil9 
coil.show()



grid = pv.UniformGrid(
    dimensions=(41, 41, 41),
    spacing=(2, 2, 2),
    origin=(-40, -40, -40),
)

# compute B-field and add as data to grid
grid["B"] = coil.getB(grid.points)
# compute field lines
seed = pv.Disc(inner=1, outer=5.2, r_res=3, c_res=12)
strl = grid.streamlines_from_source(
    seed,
    vectors='B',
    max_time=180,
    initial_step_length=0.01,
    integration_direction='both',
)

# create plotting scene
pl = pv.Plotter()

# add field lines and legend to scene
legend_args = {
    'title': 'B [mT]',
    'title_font_size': 20,
    'color': 'black',
    'position_y': 0.25,
    'vertical': True,
}

# draw coils
magpy.show(coil, color="orange", canvas=pl, backend='pyvista')

# add streamlines
pl.add_mesh(
    strl.tube(radius=.2),
    cmap="bwr",
    scalar_bar_args=legend_args,
)
# display scene
pl.camera.position=(160, 10, -10)
pl.set_background("white")
pl.show()

报错信息:

danieltran@eduroam-193-157-168-102 OneDrive-UniversitetetiOslo % /usr/local/bin/python3 "/Users/danieltran/Library/CloudStorage/OneDrive-UniversitetetiOslo/H22/FYS1120/Comp Essay/double_solenoids.py"
Traceback (most recent call last):
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/pyvista/_vtk.py", line 547, in <module>
    from vtk.vtkCommonKitPython import buffer_shared, vtkAbstractArray, vtkWeakReference
ModuleNotFoundError: No module named 'vtk'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/danieltran/Library/CloudStorage/OneDrive-UniversitetetiOslo/H22/FYS1120/Comp Essay/double_solenoids.py", line 4, in <module>
    import pyvista as pv
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/pyvista/__init__.py", line 12, in <module>
    from pyvista.plotting import *
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/pyvista/plotting/__init__.py", line 4, in <module>
    from .charts import Chart2D, ChartMPL, ChartBox, ChartPie
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/pyvista/plotting/charts.py", line 13, in <module>
    from pyvista import _vtk
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/pyvista/_vtk.py", line 549, in <module>
    from vtk.vtkCommonCore import buffer_shared, vtkAbstractArray, vtkWeakReference
ModuleNotFoundError: No module named 'vtk'. 

解决方案

优先方案:通过conda安装兼容ARM的PyVista及VTK

M2 MacBook Air是ARM64架构,pip直接安装VTK可能因缺乏预编译包失败,用conda-forge渠道可自动适配架构:

conda install -c conda-forge pyvista

备选方案1:安装PyVista完整包(含依赖)

如果使用pip环境,安装PyVista全功能版本,会自动处理VTK依赖:

pip install pyvista[all] --upgrade

备选方案2:单独安装VTK(Homebrew+Pip)

若conda不可用,先通过Homebrew安装系统级VTK,再重新关联PyVista:

brew install vtk
pip install pyvista --no-cache-dir

验证步骤

安装完成后,在Python终端执行以下命令确认:

import pyvista as pv
import vtk
print("PyVista与VTK导入成功")

无报错即可正常运行模拟代码。

内容的提问来源于stack exchange,提问作者Daniel Tran

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.13 01:25:38