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 trialNelson Chuang
1,221 PointsCan't import my class when trying to write a class in Linux
I wanted to practice writing a class to import but I get cannot import module error. Is there something we are suppose to do in order to import classes even though they are in the same folder as the script calling it?
2 Answers
Stéphane Diez
19,350 Pointsyou must have them in the right directory, for example: If you have your file in example/file.py and your importet module in example/filename/module.py you need to give the path like this in your file.py :
# some_file.py
import sys
sys.path.insert(0, '/example/filename')
import module
check this for help: http://stackoverflow.com/questions/4383571/importing-files-from-different-folder-in-python
Nelson Chuang
1,221 PointsThat's why I am confused because they are both in the same directory yet it says cannot import module. I also tried with the sys.path.insert command I got the same error. Perhaps I am not creating the module/class correctly. I gave up already and just made a function instead. Thanks anyways.