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 trialJames Ball
2,074 PointsImport from characters vs. import from characters.py
Why does "from characters import Thief" work but not "from characters.py import Thief"? Why do you not have to specify the filename extension when importing?
1 Answer
KRIS NIKOLAISEN
54,971 PointsHere is a detailed explanation of importing in Python.
Importing uses dotted paths to navigate directories so characters.py
would specify a file named py.py
in a characters
directory. I put together a workspace to demonstrate. To use enter python test.py
in the console. There are comments for each of 4 tests in test.py.
I'm still testing myself (like what happens with a file named characters.py.py, or if there is directory and file with the same name in the same directory) so I may add more later.
James Ball
2,074 PointsKris, thanks for the detailed response and workspace. I can't access the console from within the workspace you set up. What am I doing wrong?
KRIS NIKOLAISEN
54,971 PointsKRIS NIKOLAISEN
54,971 Points1) Click the link
2) In the upper right corner click 'Fork Snapshot'
3) Click the 'Fork Workspace' button. This should then load like a normal workspace
4) If the console isn't visible then choose menu View > Show Console