Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community!

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Object-Oriented Python (retired) Inheritance Subclasses

Lior Dolinski
Lior Dolinski
3,905 Points

ImportError: cannot import name ?

When i try to run one of the subclasses we've made using Import it failed..

i tried:

from monster Import Troll import monster etc,

and i got a

Traceback (most recent call last): File "<input>", line 1, in <module> ImportError: cannot import name 'Troll'

the only thing that it did manage to import was the initial Monster class..

**im using PyCharm, python 3.5.2

what could be the problem for this?

i read something about circular import dependencies but couldn't figure exactly what that was and why did Kenneth had no problem with importing the subclasses..


Lior Dolinski
Lior Dolinski
3,905 Points

Chris Freeman i think i did.. i used sys.path.insert in order to insert the projects' path into the list of libararies wich python takes data from.. does that sound reasonable? because it worked lol

Chris Freeman
Chris Freeman
Treehouse Moderator 68,404 Points

These types of errors drive me crazy. There is usually a basic answer to solve it. Maybe we're not seeing the whole problem. Your fix seems like too much overhead. If you want to debug further, could you provide a directory listing for where you are when you get the error, plus any subdirectories? Try $ ls -Rl .

2 Answers

Josh Keenan
Josh Keenan
19,652 Points

Firstly check all spellings of stuff because that's the only reason stuff never works for me.

Could you post your code?

Lior Dolinski
Lior Dolinski
3,905 Points
import random

COLORS = ['yellow', 'blue', 'green', 'red']

class Monster():
    min_hit_points = 1
    max_hit_points = 1
    min_experience = 1
    max_experience = 1
    sound = 'roar'

    def __init__(self, **kwargs):
        self.hit_points = random.randint(self.min_hit_points,self.max_hit_points)
        self.exeprience = random.randint(self.min_experience,self.max_experience)
        self.color = random.choice(COLORS)

        for key, value in kwargs.items():
            setattr(self, key, value)

    def battlecry(self):
        return self.sound.upper()

class Troll(Monster):
    min_hit_points = 3
    max_hit_points = 5
    min_experience = 2
    max_experience = 6
    sound = 'growl'

class Dragon(Monster):
    min_hit_points = 5
    max_hit_points = 10
    min_experience = 6
    max_experience = 10
    sound = 'raaaaaaaaar'

class Goblin(Monster):
    max_hit_points = 3
    max_experience = 2
    sound = 'squeak'

i double checked it.. PyCharm also didn't alarm any misspells.. any ideas?


Chris Freeman
Chris Freeman
Treehouse Moderator 68,404 Points

When I save your code to models.py, then in the same directory create a file called app.py:

from models import Troll

troll = Troll()


Then run it, I get:

$ python app.py

This seems like a PyCharm setup issue. It's not the editor I use. (Go Emacs!)

Chris Freeman
Chris Freeman
Treehouse Moderator 68,404 Points

Luckily there is a new 22-minute Workshop on setting up PyCharm