为什么cant python在我的模块中找不到类?

时间:2016-12-14 03:10:33

标签: python unit-testing import

我正在编写一个包含一些单元测试的简单python包:

goal_light:
horn.py  __init__.py

tests:
context.py  horn_test.py  __init__.py

horn.py:

class Horn:
    def __init__():
        pass

context.py:

import os
import sys
sys.path.insert(0, os.path.abspath('..'))

import goal_light

horn_test.py:

from .context import goal_light

def test_construct_horn():
    horn = goal_light.Horn()

运行测试让我:

$ py.test
============================= test session starts ==============================
platform linux2 -- Python 2.7.12, pytest-2.8.7, py-1.4.31, pluggy-0.3.1
rootdir: /home/peter/example, inifile: 
collected 1 items 

tests/horn_test.py F

=================================== FAILURES ===================================
_____________________________ test_construct_horn ______________________________

    def test_construct_horn():
>       horn = goal_light.Horn()
E    AttributeError: 'module' object has no attribute 'Horn'

tests/horn_test.py:4: AttributeError
=========================== 1 failed in 0.02 seconds ===========================

我明显与

中的某些东西发生冲突

http://python-notes.curiousefficiency.org/en/latest/python_concepts/import_traps.html

但我不确定是什么。

这已被标记为

的副本

Imported module but still need to use full name

但我明确在使用该课程时建议使用全名goal_light.Horn。我错过了什么?

0 个答案:

没有答案