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! While you're at it, check out some resources Treehouse students have shared here.

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) Objects What Are Objects And Classes?

Shawndee Boyd
Shawndee Boyd
6,002 Points

import Monster

Everytime I try to import Monster, It gives me an error. What am I doing wrong?

Shawndee Boyd
Shawndee Boyd
6,002 Points

I get a traceback error: File "<stdin>", line 1, in <module>

ImportError: cannot import name 'Monster'

Stephen Hopkinson
Stephen Hopkinson
4,648 Points

I'm having the same problem. My monster.py file is as follows:

class Monster:
    hit_points = 1
    color = 'yellow'
    weapon = 'sword'

And I type the following into the console:

python
from monster import Monster

Which gives the following error:

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

7 Answers

You have to be cd into the directory where your scripts are before you run python.

I.e. If you have your scripts in C:\Python34\Scripts. First run cmd Then cd C:\Python34\Scripts Then type python Then when you get the >>> Type in: from monster import Monster

That works for me. The standard directory was C:\Windows\System32\ on cmd. And so Python couldn't find the script. It works once I started python from C:\Python34\Scripts instead.

David Bouchare
David Bouchare
9,224 Points

Can you show us the code you wrote first?

It should be along these lines:

from ... import ...

I had the same problem, and realized that it was because I was using the workspace that I still had up for the previous Python course - "Python Collections." An answer that Kenneth gave to a different question led me to try opening the workspace immediately associated with this course, and that solved the problem for me.

I had the same problem. I had just forgotten to click File and then Save monster.py after I created the Monster class.

Thomas Helms
Thomas Helms
16,816 Points

I know that this might be super old and sorry for necro'ing a dead thread, but I had the same problem. My solution was to be outside of python (at console command line), write the code, save it, check the directory that monster.py was there (you can us the command ls to list the files in the directory), then launch into Python and do 'from monster import Monster'

Had the same problem, was able to solve by saving your py file, close the workspace, then open it again. I was working in another python workspace for another class before, not sure if that was the problem.

Tony McCabe
Tony McCabe
4,889 Points

Answer is simple: from monster import Monster... seriously!