我正在尝试从Anaconda的spyder界面运行脚本1D_gaussian.py,如下所示。该程序从保存在同一文件中的moving_average.py调用函数moving_average。我正在使用python 3.7。
import numpy as np
import matplotlib
from moving_average import moving_average
#Choose number of samples n
n=100
#initiate parameters
mu, sigma = 5, 0.01
#create data array
y=np.random.normal(mu,sigma,n)
#Calculate mu assuming sigma is known using the maximum likelihood (which is minimising
#sum of squares in this case. In fact, maximisation of log likelihood is independent of sigma
#provided it remains constant between measurements i.e. need i.i.d)
#Estimates for mu using maximum likelihood reduces to finding mean of samples
mu_tilde=moving_average(y)
print(mu_tilde)
下面的脚本Moving_average.py:
将numpy导入为np
def moving_average(x):
y=np.cumsum(x).astype(float)
for i in range(len(x)):
y[i]=y[i]/(i+1)
return y
与1D_gaussian.py保存在同一文件夹中,该文件夹具有文件路径
C:\ Users \ Arida \ Desktop \ Summer 2019 \ python_scripts \ early_tests
我已将此路径添加到“工具”下的PYTHONPATH管理器,并在间谍页面右上方的下拉框中选择了此文件夹。
但是我得到一个错误
文件“ C:/ Users / Arida / Desktop / Summer 2019 / python_scripts / early_tests / 1D_gaussian.py“,第6行,在 从moving_average导入moving_average
ModuleNotFoundError:没有名为“ moving_average”的模块
为什么这不起作用?我已经注意到,当我尝试运行脚本时,下拉列表中的路径返回到C:\ Users \ Arida,所以也许它毕竟不是在正确的位置搜索?