我正在尝试从http://twistedmatrix.com/documents/current/core/howto/trial.html的“Twisted 测试驱动开发”中重现示例。
我创建了文件:
calculus/__init__.py
calculus/base_1.py
calculus/test/__init__.py
calculus/test/test_base_1.py
我修改了 PYTHONPATH:
[xxx@zzz calculus]$ PYTHONPATH="$PYTHONPATH:`pwd`/.."
我调用了试验:
[xxx@zzz twistedm]$ trial calculus.test.test_base_1
calculus
test
test_base_1 ... [ERROR]
===============================================================================
[ERROR]
Traceback (most recent call last):
File "/usr/lib/python2.7/site-packages/twisted/trial/runner.py", line 602, in loadByNames
things.append(self.findByName(name))
File "/usr/lib/python2.7/site-packages/twisted/trial/runner.py", line 406, in findByName
return reflect.namedAny(name)
File "/usr/lib/python2.7/site-packages/twisted/python/reflect.py", line 307, in namedAny
raise ObjectNotFound('%r does not name an object' % (name,))
twisted.python.reflect.ObjectNotFound: 'calculus.test.test_base_1' does not name an object
calculus.test.test_base_1
-------------------------------------------------------------------------------
Ran 1 tests in 0.199s
FAILED (errors=1)
Python 没有找到我的模块。
但是如果我调用 python shell 并尝试导入我的模块,python 会导入它们。
然后我在文件“ /usr/lib/python2.7/site-packages/twisted/python/reflect.py ”中添加了一行“ sys.path.append('/home/xxx/twistedm/calculus/..') ” ”。现在一切正常。
所以我发现 Trial 在 sys.path 中查找模块,并且在命令行上设置 PYTHONPATH 对 sys.path 没有影响。
问题是如何在调用试用时从命令行修改 sys.path。