Python 3 ModuleNotFoundError:没有名为';标段';
我试图运行一个python3单元测试脚本,但是当它使用来自主scit的对象并遇到import语句时,它给了我一个modulenotfound错误。当我运行主脚本本身并且代码按预期运行时,我不会遇到这个错误。我得到的错误回溯是:Python 3 ModuleNotFoundError:没有名为';标段';,python,python-3.x,Python,Python 3.x,我试图运行一个python3单元测试脚本,但是当它使用来自主scit的对象并遇到import语句时,它给了我一个modulenotfound错误。当我运行主脚本本身并且代码按预期运行时,我不会遇到这个错误。我得到的错误回溯是: Traceback (most recent call last): File "parkingLot_test.py", line 3, in <module> from source import parking File "/home
Traceback (most recent call last):
File "parkingLot_test.py", line 3, in <module>
from source import parking
File "/home/stash/Desktop/parking-lot/parking-lot-1.4.2/parking_lot/parking_lot/bin/source/parking.py", line 1, in <module>
import lot
ModuleNotFoundError: No module named 'lot'
单元测试文件的代码parkingLot\u test.py:
import unittest
from source import parking
class ParkingTest(unittest.TestCase):
@classmethod
def MakeClass(cls):
cls.parking = parking.Parking()
cls.allocated_slot = 1
def test_a_create_parking_lot(self):
parking_slots = 6
self.parking.create_parking_lot(parking_slots)
self.assertEqual(len(self.parking.slots), parking_slots,
msg="Wrong parking lot created")
def test_b_park(self):
registration_no = "MH-12-FF-2017"
colour = "Black"
self.parking.park(registration_no, colour)
self.assertFalse(
self.parking.slots[self.allocated_slot].available, "Park failed.")
for i in self.parking.slots.values():
if not i.available and i.car:
self.assertEqual(i.car.registration_no,
registration_no, "Park failed")
self.assertEqual(i.car.colour, colour, "Park failed")
def test_c_leave(self):
self.parking.leave(self.allocated_slot)
self.assertTrue(
self.parking.slots[self.allocated_slot].available, "Leave failed.")
@classmethod
def RemoveClass(cls):
del cls.parking
if __name__ == '__main__':
unittest.main()
import lot
import car
class Parking:
"""
Parking class which has details about parking slots
as well as operation performed on parking are present here
"""
def __init__(self):
self.slots = {}
def create_parking_lot(self, no_of_slots):
"""This method will create parking lot if not present already with given
number of slots.
Input: no_of_slots - Integer Type
"""
no_of_slots = int(no_of_slots)
if len(self.slots) > 0:
print("Parking Lot already created")
return
if no_of_slots > 0:
for i in range(1, no_of_slots+1):
temp_slot = lot.ParkingSlot(slotNum=i,
availability=True)
self.slots[i] = temp_slot
print("Created a parking lot with %s slots" % no_of_slots)
else:
print("Number of slots provided is incorrect.")
return
def get_nearest_available_slot(self):
...................
导入文件parking.py的代码如下:
import unittest
from source import parking
class ParkingTest(unittest.TestCase):
@classmethod
def MakeClass(cls):
cls.parking = parking.Parking()
cls.allocated_slot = 1
def test_a_create_parking_lot(self):
parking_slots = 6
self.parking.create_parking_lot(parking_slots)
self.assertEqual(len(self.parking.slots), parking_slots,
msg="Wrong parking lot created")
def test_b_park(self):
registration_no = "MH-12-FF-2017"
colour = "Black"
self.parking.park(registration_no, colour)
self.assertFalse(
self.parking.slots[self.allocated_slot].available, "Park failed.")
for i in self.parking.slots.values():
if not i.available and i.car:
self.assertEqual(i.car.registration_no,
registration_no, "Park failed")
self.assertEqual(i.car.colour, colour, "Park failed")
def test_c_leave(self):
self.parking.leave(self.allocated_slot)
self.assertTrue(
self.parking.slots[self.allocated_slot].available, "Leave failed.")
@classmethod
def RemoveClass(cls):
del cls.parking
if __name__ == '__main__':
unittest.main()
import lot
import car
class Parking:
"""
Parking class which has details about parking slots
as well as operation performed on parking are present here
"""
def __init__(self):
self.slots = {}
def create_parking_lot(self, no_of_slots):
"""This method will create parking lot if not present already with given
number of slots.
Input: no_of_slots - Integer Type
"""
no_of_slots = int(no_of_slots)
if len(self.slots) > 0:
print("Parking Lot already created")
return
if no_of_slots > 0:
for i in range(1, no_of_slots+1):
temp_slot = lot.ParkingSlot(slotNum=i,
availability=True)
self.slots[i] = temp_slot
print("Created a parking lot with %s slots" % no_of_slots)
else:
print("Number of slots provided is incorrect.")
return
def get_nearest_available_slot(self):
...................
如何解决此问题?因为python 3不支持隐式相对导入。可以使用显式相对导入 尝试使用以下行,而不是parking.py中的import lot
来自。在同一目录级文件中导入批次
当您在另一个目录级文件中导入一个文件时,请使用相对导入,如
在parking.py
文件中导入lot.py
文件为
import .log
当从一个目录导入到下一个目录时,即在
parkingLot\u test.py
,使用源导入批次的
并在\uuuu init\uuuuuupy
文件写入的源文件夹中
import .lot
import .parking
像这样 文件夹
source
需要添加到导入路径,方法是将其路径附加到sys.path
或将其放入PYTHONPATH
@rdas,因为我仍然得到ModuleNotFoundError:No module name'lot'
我使用的sys.path.append(./”)
在import-lot语句之前,尝试直接运行parking.py@rdas。尝试使用此命令运行脚本python-m parkingLot\u test
我收到一个错误ImportError:尝试在没有已知父包的情况下进行相对导入
在主脚本和使用python-m parkingLot\u test
除了一些python3语法差异错误外,测试脚本似乎运行良好。因此,它似乎按照Python2.7的预期运行,当直接运行脚本时,它没有设置\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu。如果您使用-m标志运行脚本,它会告诉解释器将文件视为一个模块。如果您没有在lot.py中提到\uu all\uuuu
,我会得到这个错误AttributeError:module'source.lot'没有属性'\uu all\uuuuuu'
,只需从\uu init\uuuuuuuuuuuy.py
文件中删除\uu all\uuuuuuuu
,是的,您可以在\uuuu init\uuuuuuuuuuuuuuuuuuupy
文件中写入并进行更改,我在解决方案中进行了更改,就像这样写入\uuuuuuuuuuuuuuuuuuuuupy
文件我尝试了方法,但我得到错误AttributeError:“ParkingTest”对象没有属性“parking”
来自源代码导入parking