How to Fix AttributeError: module 'magpylib" has no attribute 'source'

How to Fix AttributeError: module 'magpylib" has no attribute 'source'

我已经尝试将 magpylib 模块安装到 VSCode 和 Anaconda(Spyder) 上,但收到了相同的错误。我对编程比较陌生,不知道如何解决,我也没有发现类似的问题。对于代码,我使用了 site.

给出的第一个示例代码
# imports
import numpy as np
import matplotlib.pyplot as plt
import magpylib as magpy

# create magnets
magnet1 = magpy.source.magnet.Box(mag=[0,0,600],dim=[3,3,3],pos=[-4,0,3])
magnet2 = magpy.source.magnet.Cylinder(mag=[0,0,500], dim=[3,5], pos=[0,0,0])

# manipulate magnets
magnet1.rotate(45,[0,1,0],anchor=[0,0,0])
magnet2.move([5,0,-4])

# collect magnets
pmc = magpy.Collection(magnet1,magnet2)

# display system geometry
pmc.displaySystem()

# calculate B-fields on a grid
xs = np.linspace(-10,10,20)
zs = np.linspace(-10,10,20)
Bs = np.array([[pmc.getB([x,0,z]) for x in xs] for z in zs])

# display fields using matplotlib
fig, ax = plt.subplots()
X,Y = np.meshgrid(xs,zs)
U,V = Bs[:,:,0], Bs[:,:,2]
ax.streamplot(X, Y, U, V, color=np.log(U**2+V**2), density=1.5)
plt.show() 

解决方法如下:

import magpylib as magpy

magnet1 = magpy.magnet.Box(magnetization=[0, 0, 600], dimension=[3, 3, 3], position=[-4, 0, 3])

您可能找到了一个已弃用的示例。

对于您以后的问题,请提供最少的代码,即以最少的行数和您的 python 版本重现错误的代码。在你的情况下:

import magpylib as magpy

magnet1 = magpy.source.magnet.Box(mag=[0, 0, 600], dim=[3, 3, 3], pos=[-4, 0, 3])