Python 导入错误:没有名为';蓝图';
我正在为一个暑期班学习Python,遇到了这个错误:在下面的代码中,您将看到我有一个包,我将它加载到Director.py中,我将它用作我的主文件。但是,如果BluePrints.py明显位于目录中,它就会丢失。我也检查了很多次输入错误,但这不是问题所在。在我正在学习的书中没有关于这个问题的文档,也没有在Python文档在线中看到的文档,在StackOverflow上也找不到这个特定的问题。这是我的密码:Python 导入错误:没有名为';蓝图';,python,module,builder,importerror,Python,Module,Builder,Importerror,我正在为一个暑期班学习Python,遇到了这个错误:在下面的代码中,您将看到我有一个包,我将它加载到Director.py中,我将它用作我的主文件。但是,如果BluePrints.py明显位于目录中,它就会丢失。我也检查了很多次输入错误,但这不是问题所在。在我正在学习的书中没有关于这个问题的文档,也没有在Python文档在线中看到的文档,在StackOverflow上也找不到这个特定的问题。这是我的密码: # Director.py from Builder import HouseBuilde
# Director.py
from Builder import HouseBuilders as hb, HouseBuilderInterface as hbi
interface = hbi.HouseBuilderInterface() # An interface to my concrete builders
first_house = hb.EarthHomeBuilder() # Initialize a concrete builder
interface.build_room(first_house, 'kitchen') # Build a room in the Earth House
Builder/HouseBuilderInterface.py
class HouseBuilderInterface():
""" HouseBuilder utilizes Python's duck-typing to provide an interface to the concrete house builders"""
def build_room(obj, room_type): # Rooms are an attribute shared by all houses
obj.build_room(room_type)
def build_wall(obj, room1, room2): # Walls are an attribute shared by all houses; Duplex's will have a unique Dividing Wall
obj.build_wall(room1, room2)
def build_window(obj, wall): # Windows are an attribute shared by all houses
obj.build_window(wall)
def build_door(obj): # Doors are an attribute shared by all houses; Colonial and Split-level houses can have basement doors
obj.build_door()
def build_stairs(obj): # Stairs can only be built in Colonial and Split-level houses; Colonial stair cases are bigger
obj.build_stairs()
def build_extra(obj): # Some houses have extra features: Earth-homes have plants on the roof, Single-story has a wrap-around-porch, etc.
obj.build_extra()
建筑商/房屋建筑商.py
import BluePrints
class EarthHomeBuilder():
def __init__(self): # Initialize a simple house
self.room_no = 0
self.window_no = 0
self.door_no = 1
self.wall_no = 4
self.key = 0 # key attribute is a unique key for the dictionary
self.house_features = {}
def build_room(self, room_type):
new_room = BluePrints.Room(room_type)
self.room_no += 1
self.house_features[key] = room_type + ''
self.key += 1
Builder/BluePrints.py
class Room():
""" Defines a room object with an attribute of room_type"""
def __init__(self, room_type):
self.room_type = room_type
class Wall():
""" Defines a Wall object with attributes of window and door """
def __init__(self): # Initialize windows & doors to 0
self.windows = 0
self.doors = 0
def add_window(self):
self.windows += 1
def add_door(self):
self.doors += 1
class Window():
""" Defines a Window object with no attributes; Adds a window to specified Wall object"""
def __init__(wall_location):
wall_location.add_window() # Use duck-typing to call Wall method add_window()
class Door():
""" Defines a Door object with no attributes; Adds a door to specified Wall object """
def __init__(wall_location):
wall_location.add_door() # Use duck-typing to call Wall method add_door()
class StairCase():
""" Defines a StairCase object with an attribute of length """
def __init__(self, length):
self.length = length
class ExtraFeature():
""" Defines an ExtraFeature object which is unique type of house feature specific to each house """
def __init__(self, extra_feature):
self.extra_feature = extra_feature
注意:此任务是Builder软件模式的实现。此外,我只测试了build_room方法,如Director.py文件所示。我不关心任何bug,除非它们与导入BluePrints.py的问题有关。确切的错误消息是ImportError:没有名为“BluePrints”的模块
编辑:
我正在使用Python3.4问题是,在您将
HouseBuilders
导入Director.py之后,sys.path
仍然保留在Director.py
的目录中,因此在sys.path
指定的任何目录中都没有直接的BluePrints.py
python在试图查找要导入的包或类时,会查找sys.path
。我猜您正在使用Python3.x
解决办法是按照政府说的去做-
6.4.2。包内引用
当包被构造成子包时(与示例中的声音包一样),可以使用绝对导入来引用同级包的子模块。例如,如果模块sound.filters.vocoder需要使用sound.effects包中的echo模块,它可以使用from sound.effects import echo
因此,在Builder/HouseBuilders.py
中,将导入语句更改为-
from Builder import BluePrints
你在
Builder
目录中有\uuu init\uuuuuuuuuuuuuuuuupy
文件吗?@Alik我相信他有,否则错误应该是无法导入名称“HouseBuilders
@AnandSKumar,在重新阅读问题并看到后,问题是关于BluePrints.py
模块的,我得出结论,你是对的。请发布准确的错误和完整的回溯。另外,如果您向我们展示如何执行Directory.py
,如果Builder
是一个包,那么import BluePrints
应该可以正常工作,而无需修改sys.path
。文档的相关部分是@Alik,你试过了吗?这对我不起作用,让我试着挖一点deeper@Alik看起来OP使用的是Python3.x,但在Python3.x中有一些变化。谢谢。如果我理解正确,sys.path会找到包含Director.py的文件夹。此模块从包生成器导入除BluePrints.py以外的所有文件。HouseBuilders.py现在位于sys.path中与Directory.py相同的文件夹中,它尝试从当前文件夹导入BluePrints.py。但是,如果没有“from Builder”命令,就无法导入BluePrints.py。这是正确的吗?第二部分你是正确的,第一部分不,Housebuilders.py与Directory.py不在同一个文件夹中,python正确地知道在哪里查找它。但是在HouseBuilders.py
内部,当您导入蓝图时,python认为您试图从与Director.py
相同的文件夹导入BluePrints.py
,但python找不到任何此类文件,因此给出了错误。Python这样想是因为您正在运行Director.py
script。