为什么 Python 在我的模块中找不到第二个函数?

Why can't Python find the second function in my module?

极端Python 新手在这里。我正在尝试为 LibreOffice Calc 的 Black-Scholes option/greek 公式编写一些函数。我想制作一个具有各种功能的大模块,我需要在几个电子表格中使用。我将它们保存在一个名为 TradingUtilities.py 的文件中。前两个函数看起来像

def BSCall(S, K, r, sig, T):
    import scipy.stats as sp
    import numpy as np
    d1 = (np.log(S/K) - (r - 0.5 * sig * sig) * T)/(sig*np.sqrt(T))
    d2 = d1 - sig*np.sqrt(T)
    P1 = sp.norm.cdf(d1)
    P2 = sp.norm.cdf(d2);
    call = S*P1 - K * np.exp(-r*T)*P2;
    return call
def BSPUt(S, K, r, sig, T):
    import scipy.stats as sp
    import numpy as np
    d1 = (np.log(S/K) - (r-0.5*sig*sig)*T)/(sig*np.sqrt(T))
    d2 = d1 - sig*np.sqrt(T)
    P1 = sp.norm.cdf(-d1)
    P2 = sp.norm.cdf(-d2)
    put = K*exp(-r*t)*P2 - S*P1
    return put

当我从命令行运行脚本时,第一个函数工作正常。但是当我尝试 运行 第二个

时出现以下错误
>>> import TradingUtilities as tp
>>> tp.BSCall(238, 238.5, 0.05, 0.09, 0.09)
2.9860730330243541
>>> tp.BSPut(238, 238, 0.05, 0.09, 0.09)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: module 'TradingUtilities' has no attribute 'BSPut'

我正在尝试找出问题所在,但到目前为止还没有成功。如果有人能看出我做错了什么,或者指出正确的方向,我将不胜感激。

谢谢!

你的代码有错别字

>>> tp.BSPut(238, 238, 0.05, 0.09, 0.09)

应该是

>>> tp.BSPUt(238, 238, 0.05, 0.09, 0.09)

或者您可以在主代码中将 BSPUt 更改为 BSPut

与您在命令行中编写的名称相比,您的函数名称存在拼写错误。 Python 区分大小写。

应该是: def BSPut(S, K, r, sig, T):