我正在学习Python并且来自C#后台查找包和模块有点复杂且难以理解。 我使用Visual Studio 2017编辑我的项目。 我有一个像这样的项目设置:
./tests/__init__.py
from TestOne import TestOne
from TestTwo import TestTwo
./tests/TestOne.py
from base import BaseTest
class TestOne(BaseTest):
def __init__():
pass
./tests/TestTwo.py
from base import BaseTest
class TestTwo(BaseTest):
def __init__():
pass
./base.py
class BaseTest:
def __init__():
pass
./main.py
from tests import TestOne
from tests import TestTwo
a = TestOne()
b = TestTwo()
当我执行python main.py时,我收到以下错误:
Traceback (most recent call last):
File "D:\Code\main.py", line 2, in <module>
from tests import TestOne
File "D:\Code\tests\__init__.py", line 1, in <module>
from TestOne import TestOne
ModuleNotFoundError: No module named 'TestOne'
你能帮帮我吗?这样做的原因是我将有超过9000个测试,每个测试将有超过9000行代码。我觉得很难习惯每个文件(模块)pythonic方式的多个类,除了在一个文件中有9000 ^ 2行是完全不可能的:)
编辑:
在tests/__init__.py
前加上:
import sys
sys.path.insert(0, __path__[0])
似乎解决了我的问题。但这似乎是一个黑客。我还意味着每个软件包都应该包含在它的初始脚本中,但我不认为是这样的。 我仍然想知道为什么会这样。