Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
在我的试用测试用例中,我想从我的源代码树中运行脚本。试用会更改工作目录,因此简单的相对路径不起作用。在实践中,Trial 的临时目录位于源代码树中,但假设是这种情况似乎不是最理想的。即,我可以这样做:
def source_file(p): return os.path.join('..', p)
有没有更好的办法?
如果您想在测试旁边找到一个文件并将其作为脚本运行,您可以这样做:
from twisted.python.modules import getModule script = getModule(__name__).filePath.path # ... reactor.spawnProcess(..., script, ...)
您也可以使用它来支持将代码存储在 zip 文件中,尽管使用 Python 调用它会变得有点困难。你考虑过只用python -m吗?
python -m