2

我有以下文件夹结构

project
  +folder src
    +file main.py
    +file __init__.py
  +folder tests
    +file test.py
    +file __init__.py
  +file __init__.py

所有__init__文件都是空的。

现在我希望能够在任何地方test.py运行表单,这很重要。

test.py来电

import src

因为它需要来自main.py. 这会导致错误:

ImportError: no module named src

我找到的解决方案包括向我的环境变量添加sys.path.append命令test.py或将文件夹 src 添加到我的PATH环境变量中 - 没有其他方法吗?

我不能强迫每个用户更改他们的PATH,也不能向所有测试文件添加sys.path.append命令。

4

1 回答 1

1

您可以创建应用程序包并将其安装在运行测试的环境中。请参阅此处获取教程http://python-packaging.readthedocs.org/en/latest/

于 2015-11-20T15:41:26.547 回答