Python Object-Oriented Python (retired) Inheritance Subclasses

Lior Dolinski
Lior Dolinski
3,683 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..

THANKS!

Lior Dolinski
Lior Dolinski
3,683 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 51,421 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
12,555 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,683 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?

Thanks!

Chris Freeman
MOD
Chris Freeman
Treehouse Moderator 51,421 Points

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

app.py
from models import Troll

troll = Troll()

print(troll.sound)

Then run it, I get:

$ python app.py
growl

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

Chris Freeman
Chris Freeman
Treehouse Moderator 51,421 Points

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