1

I "relative import" some module in my py file and test file.

from .. import Message
from .Server import StupServerProtocol

It works perfectly with py.test, but when I turn to twisted trial, everything goes wrong.

[ERROR]
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/site-packages/twisted/trial/runner.py", line 605, in loadByNames
    things.append(self.findByName(name))
  File "/usr/local/lib/python2.7/site-packages/twisted/trial/runner.py", line 408, in findByName
    return filenameToModule(name)
  File "/usr/local/lib/python2.7/site-packages/twisted/trial/runner.py", line 96, in filenameToModule
    return _importFromFile(fn)
  File "/usr/local/lib/python2.7/site-packages/twisted/trial/runner.py", line 120, in _importFromFile
    module = imp.load_source(moduleName, fn, fd)
  File "Server_test.py", line 12, in <module>
    from .. import Message
exceptions.ValueError: Attempted relative import in non-package

And my directory tree is like this:

├── Message
│   ├── __init__.py
├── Protocol
│   ├── Server.py
│   ├── Server_test.py (* i'm want to run this)
│   ├── __init__.py
├── Utils.py
├── Worker
│   ├── RawWorker.py
│   ├── WorkerBase.py
│   ├── __init__.py

Is there something like pytest to replace trial? Or some work around to make trial happy?

Thanks.

Wizmann
  • 839
  • 1
  • 9
  • 14
  • You can append the path where your module is to ``sys.path``. You can also consider using the builtin ``imp.load_module`` function. http://stackoverflow.com/questions/10772847/imports-working-with-raw-file-but-not-in-idle – notorious.no Jun 10 '16 at 17:56
  • @notorious `sys.path.append` does not work because I've used relative import in many other modules. – Wizmann Jun 11 '16 at 01:20

0 Answers0