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 trialShawndee Boyd
6,002 Pointsimport Monster
Everytime I try to import Monster, It gives me an error. What am I doing wrong?
Stephen Hopkinson
4,648 PointsI'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
Jeyananthan Sivakumaran
6,728 PointsYou 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
9,224 PointsCan you show us the code you wrote first?
It should be along these lines:
from ... import ...
Bret Runestad
3,376 PointsI 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.
paulthomson3
4,151 PointsI had the same problem. I had just forgotten to click File and then Save monster.py after I created the Monster class.
Thomas Helms
16,816 PointsI 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'
Ginny Gao
4,300 PointsHad 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
4,889 PointsAnswer is simple: from monster import Monster... seriously!
Shawndee Boyd
6,002 PointsShawndee Boyd
6,002 PointsI get a traceback error: File "<stdin>", line 1, in <module>
ImportError: cannot import name 'Monster'