AttributeError:'module'对象没有python方法的属性

时间:2017-08-19 10:53:47

标签: python-2.7 pydev python-unittest

我最近通过apt:

在Ubuntu 16.04上安装了QuantLib-Python
sudo apt-get install -y libquantlib0-dev libquantlib0v5 quantlib-python

似乎当我在Eclipse PyDev上通过 unittest 调用QuantLib时,我收到以下错误:

AttributeError: 'module' object has no attribute 'Date'

但是,我可以通过终端或Eclipse中的常规 Python脚本或Eclipse中的 Pytest 成功执行QuantLib。请参阅以下示例代码:

>>> import QuantLib as ql
>>> print ( ql.Date(1,1,2010) )
January 1st, 2010

QuantLib共享库安装在以下文件夹中:

$ ls /usr/lib/python2.7/dist-packages/QuantLib/
init.py init.pyc QuantLib.py QuantLib.pyc _QuantLib.so

QuantLib文件夹包含在我的sys.path中,包括Eclipse和终端。输出如下:

/usr/lib/python2.7
/usr/lib/python2.7/dist-packages
/usr/lib/python2.7/lib-dynload
/usr/lib/python2.7/lib-old
/usr/lib/python2.7/lib-tk
/usr/lib/python2.7/plat-x86_64-linux-gnu
/usr/local/lib/python2.7/dist-packages
/usr/local/lib/python2.7/dist-packages/Mako-1.0.7-py2.7.egg
/usr/local/lib/python2.7/dist-packages/MarkupSafe-1.0-py2.7-linux-x86_64.egg
/usr/local/lib/python2.7/dist-packages/appdirs-1.4.3-py2.7.egg
/usr/local/lib/python2.7/dist-packages/cx_Oracle-6.0rc2-py2.7-linux-x86_64.egg
/usr/local/lib/python2.7/dist-packages/decorator-4.1.2-py2.7.egg
/usr/local/lib/python2.7/dist-packages/pip-9.0.1-py2.7.egg
/usr/local/lib/python2.7/dist-packages/py-1.4.34-py2.7.egg
/usr/local/lib/python2.7/dist-packages/pycuda-2017.1-py2.7-linux-x86_64.egg
/usr/local/lib/python2.7/dist-packages/pytest-3.1.3-py2.7.egg
/usr/local/lib/python2.7/dist-packages/pytools-2017.4-py2.7.egg   

有没有人知道为什么QuantLib模块通过unittest框架失败了?

PS:在我发现它仅通过unittest失败后,这篇文章被修改了。

1 个答案:

答案 0 :(得分:0)

单元测试夹具显然写在以下文件 QuantLib.py (这是错误!!!),而我的命名约定是TestNamesomething,PyTest是以正确的方式编写的。显然,这让翻译感到困惑,最终这是我注意到的最后一件事。我将unitest文件更改为除QuantLib.py以外的其他文件,它们都恢复正常。测试结果如下:

import unittest
import QuantLib as ql

class QuantLibFixture(unittest.TestCase):
    def test_dates(self):
        date1 = ql.Date(1,12,2017)

if __name__ == '__main__':
    unittest.main()

文件更正前的单元测试错误:

>>> python QuantLib.py
E
======================================================================
ERROR: test_dates (__main__.QuantLibFixture)
----------------------------------------------------------------------
Traceback (most recent call last):
File "QuantLib.py", line 12, in test_dates
    date1 = ql.Date(1,12,2017)
AttributeError: 'module' object has no attribute 'Date'
FAILED (errors=1)

校正后的单位测试运行:

test_dates (tests.PyLib.Adhoc.TestQuantLib.QuantLibFixture) ... ok
----------------------------------------------------------------------
Ran 1 test in 0.000s
OK