Question

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?

Was it helpful?

Solution

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?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top