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

monster.py answer: ImportError: No module named 'monster'

Not a question, but an answer in case anyone else came across the ImportError: No module named 'monster' error when developing outside workspaces.

In Python, if you are saving your files outside where Python knows files to be, it will be necessary to modify your path.

I use IDLE and all my files live in my F: drive and my Python install didn't know about that location, so when trying to...

from monster import Monster

I would get...

ImportError: No module named 'monster'

I was able to solve this by importing sys and appending the path to my files directly into the editor.

import sys
print(sys.path)
sys.path.append("F:\\USERS\BR\Documents\PYTHON\OBJECTS")
print(sys.path)

It should be noted that the interpreter didn't seem to like the path being pasted into the editor and gave me a 'unicodeescape' error. I solved this by manually typing in the path.

It also should be noted that these settings are temporary and only last until you restart the shell..

2 Answers

Kenneth Love
STAFF
Kenneth Love
Treehouse Guest Teacher

Hmm. So long as your files are all in the same directory, and you're running your script from there, Python 3 should be able to find the files. Are you, by chance, using an older version of Python, maybe 2.7?

Its Python 3.4.2, but I launch IDLE from inside the the python 3.4 lib folder. I am still searching for a more permanent solution.

gregory fenwick
gregory fenwick
7,569 Points

I just noticed the same problem on my MAC, running from the native terminal. My scripts are in a separate projects file I created. When I run python3 from with that directory and try and use the from monster import Monster the same error is apparent.

from monster import Monster Traceback (most recent call last): File "<stdin>", line 1, in <module> ImportError: No module named 'monster'

import sys print(sys.path) sys.path.append("/projects/python") print(sys.path)

print(sys.path) ['', '/usr/local/Cellar/python3/3.5.1/Frameworks/Python.framework/Versions/3.5/lib/python35.zip', '/usr/local/Cellar/python3/3.5.1/Frameworks/Python.framework/Versions/3.5/lib/python3.5', '/usr/local/Cellar/python3/3.5.1/Frameworks/Python.framework/Versions/3.5/lib/python3.5/plat-darwin', '/usr/local/Cellar/python3/3.5.1/Frameworks/Python.framework/Versions/3.5/lib/python3.5/lib-dynload', '/usr/local/lib/python3.5/site-packages', '/projects/python'] from monster import Monster Monster.color 'yellow'

Python 3.5.1 (default, Jan 22 2016, 17:08:33) [GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.57)] on darwin Type "help", "copyright", "credits" or "license" for more information.

Very curious?