[英]How to Fix AttributeError: module 'magpylib" has no attribute 'source'
I have tried installing the magpylib module onto both VSCode and Anaconda(Spyder) and have received identical errors.我尝试将 magpylib 模块安装到 VSCode 和 Anaconda(Spyder) 上,并收到相同的错误。 I am relatively new to programming and do not know how to fix it, neither have I found a similar question.我对编程比较陌生,不知道如何解决它,我也没有找到类似的问题。 For the code I used the first example code given by the 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()
Here is the solution:这是解决方案:
import magpylib as magpy
magnet1 = magpy.magnet.Box(magnetization=[0, 0, 600], dimension=[3, 3, 3], position=[-4, 0, 3])
You might have found a deprecated example.您可能已经找到了一个已弃用的示例。
For your future questions, please provide a minimal code, ie a code reproducing the error in a minimum of lines AND you python version.对于您未来的问题,请提供最少的代码,即以最少的行数重现错误的代码和您的 python 版本。 In your case :在你的情况下:
import magpylib as magpy
magnet1 = magpy.source.magnet.Box(mag=[0, 0, 600], dim=[3, 3, 3], pos=[-4, 0, 3])
尝试安装旧版本的 magpylib 例如:
pip install magpylib==2.3.0b0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.