我正在尝试重现来自 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”。现在一切正常。
所以我发现 trial 在 sys.path 中查找模块,并且在命令行上设置 PYTHONPATH 对 sys.path 没有影响。
问题是在调用 trial 时如何从命令行修改 sys.path。
最佳答案
或者:
PYTHONPATH="${PWD}" trial calculus
或者:
python -m twisted.trial calculus
或者编写一个 setup.py
并安装您的源代码(可能使用 pip -e
将其安装到 virtualenv 中),然后:
trial calculus
关于python - Twisted 试验、PYTHONPATH 和 sys.path,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42425901/