I am in the process of learning Python for a summer class, and have encountered this error: In the following code you will see I have a package which I load into Director.py, which I am using as my main file. However, BluePrints.py is missing when it is clearly in the directory. I have also checked numerous times for typing errors, but that is not the issue. There is no documentation on this issue in the book I am learning from, none that I can see in the Python documentation online, and cannot find this specific problem on StackOverflow. Here is my code:
# 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()
Builder/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
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
Note: This assignment is an implementation of the Builder software pattern. Also, I have only been testing the build_room method, as seen in the Director.py file. I am not concerned with any bugs unless they pertain to the issue importing BluePrints.py The exact error message is ImportError: no module named 'BluePrints'
I am using Python3.4
The issue is that after you have imported HouseBuilders
into your Director.py , the sys.path
still remains at the directory of the Director.py
, and hence there are no BluePrints.py
directly in any directory specified by sys.path
.
And it is sys.path
that python looks up when trying to find a package or class to import. Also I am guessing you are using Python 3.x .
A fix would be to do what the documentation says -
6.4.2. Intra-package References
When packages are structured into subpackages (as with the sound package in the example), you can use absolute imports to refer to submodules of siblings packages. For example, if the module sound.filters.vocoder needs to use the echo module in the sound.effects package, it can use from sound.effects import echo.
So in Builder/HouseBuilders.py
, change your import statement to -
from Builder import BluePrints