我以这个例子为例:
https://www.jetbrains.com/help/pycharm/pytest.html
我在src/project下创建了两个类:car.py和test_car_py test.py:
class Car(object):
def __init__(self, speed=0):
self.speed = speed
self.odometer = 0
self.time = 0
...
然后在test_car_py test.py中:
from Car import Car
def test_car_brake():
car = Car(50)
assert car.speed == 45
在pycharm中,import语句显示“car”是一个未解析的引用。当我试图运行它时,出现了以下错误:
============================= test session starts ==============================
platform darwin -- Python 3.6.8, pytest-4.2.0, py-1.7.0, pluggy-0.8.1
rootdir: /Users/minn/PycharmProjects/test/src, inifile:
test_car_pytest.py:None (test_car_pytest.py)
ImportError while importing test module '/Users/minn/PycharmProjects/test/src/test_car_pytest.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
test_car_pytest.py:1: in <module>
from Car import Car
E ModuleNotFoundError: No module named 'Car'
当我单独执行car.py时,它运行正常。这两个文件在同一目录下,为什么导入失败?