Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Python Object-Oriented Python Instant Objects Let's Make a Class!

James Ball
James Ball
2,073 Points

Import 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) 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

1 Answer

Here 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
James Ball
2,073 Points

Kris, 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?