In my trial test case, I want to run scripts from my source tree. Trial changes the working directory, so simple relative paths don't work. In practice, Trial's temporary directory is inside the source tree, but assuming that to be the case seems suboptimal. I.e., I could do:

def source_file(p):
    return os.path.join('..', p)

Is there a better way?

有帮助吗?

解决方案

If you want to find a file next to your test and run it as a script, you can just do this:

from twisted.python.modules import getModule
script = getModule(__name__).filePath.path
# ...
reactor.spawnProcess(..., script, ...)

You can also use this to support storing your code in a zip file, although invoking it with Python becomes a little more difficult that way. Have you considered just using python -m?

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top