繁体   English   中英

导入错误:没有名为“ BluePrints”的模块

[英]Import Error: No Module named 'BluePrints'

我正在学习夏季课程的Python,并且遇到了此错误:在以下代码中,您将看到我有一个加载到Director.py的程序包,它将用作我的主文件。 但是,当BluePrints.py明显位于目录中时,它会丢失。 我也检查了无数次键入错误,但这不是问题。 我正在学习的书中没有关于此问题的文档,我在网上的Python文档中也看不到任何文档,并且在StackOverflow上找不到此特定问题。 这是我的代码:

# 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

生成器/ 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()

生成器/ HouseBuilders.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

生成器/ 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软件模式的实现。 另外,正如Director.py文件中所示,我仅在测试build_room方法。 我不关心任何错误,除非它们与导入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导入echo。

因此,在Builder/HouseBuilders.py ,将导入语句更改为-

from Builder import BluePrints

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM