0

我目前正在开发一个具有以下文件结构的项目:

  • 模块
    • 设备 1
    • 设备 2
  • 测试
    • 测试 1

最终,我无法在我的测试用例中导入Device 1和。目前这是通过将父目录添加到路径来实现的:. 不幸的是,这会阻止我的 IDE 向我显示设备的有用信息。相对导入在这里似乎不起作用,因为它是主文件,即它以. 我正在使用python 2.7。Device 2Test 1sys.path.append(os.path.join(os.path.dirname(__file__), ".."))Test 1python test1.py

4

1 回答 1

0

I'm doing this in a way that I'm not 100% is standard, but it's working for the meantime: Screenshot of sample project

So, as you can see, I don't have a main.py in this mock-up, yet. What I do have are modules in one folder and tests in the other.

The main "trick" (and, again, I'm not sure it's advisable) is to climb down the folder hierarchy using sys.path.append and __file__.rsplit('\',2). The other trick is that, if called as main, every module loads up the init and tests itself, but if the init file is called, it tests all of the modules.

Hopefully someone can show you a better solution, but this may work in a pinch.

于 2013-03-05T20:43:52.083 回答