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

时间:2017-04-30 14:25:30

标签: python module python-import

极端Python新手在这里。我试图为LibreOffice Calc的Black-Scholes选项/希腊公式编写一些函数。我想制作一个具有各种功能的大模块,我需要在一些电子表格中使用。我把它们保存在一个名为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'

我正在试图找出错误,但到目前为止没有运气。如果有人能够看到我做错了什么,或者指出了我正确的方向,我会非常感激。

谢谢!

2 个答案:

答案 0 :(得分:1)

您的代码中存在拼写错误

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

应该是

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

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

答案 1 :(得分:0)

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

它应该是: <input type='$type' name='$row[cid]' class='form-control' placeholder='$type...' required value=''>

相关问题