gpt4 book ai didi

Python 3 模块未找到错误 : No module named 'lot'

转载 作者:太空宇宙 更新时间:2023-11-03 23:58:42 25 4
gpt4 key购买 nike

我正在尝试运行一个 python3 单元测试脚本,但是当它使用来自主 scipt 的对象并遇到 import 语句时,它给了我一个模块未找到错误。当我运行主脚本本身并且代码按预期运行时,我不会遇到此错误。我得到的错误回溯是:

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'

目录结构如下:

├── file_inputs.txt
├── parking_lot
├── parkingLot_test.py
├── run_functional_tests
├── setup
└── source
├── car.py
├── __init__.py
├── lot.py
├── main.py
├── parking.py

单元测试文件parkingLot_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()

导入文件parking.py的代码:

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

来自 .导入批处理

关于Python 3 模块未找到错误 : No module named 'lot' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56536673/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com