문제

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