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 Object-Oriented Python Instant Objects Let's Make a Class!

further details of of Import.

  1. How exactly the import function works? why do we need it?
  2. How do you know where to import from?

2 Answers

Steven Parker
Steven Parker
229,732 Points

The import function pulls in code from other files. It helps keep your code easy to work with by not cluttering it up with common library modules.

Sometimes you may import from files you created yourself. But otherwise, you will learn which functions you might want to use are located in which standard modules and import the ones you need. As you progress in the courses you will learn many of the most useful ones. And as you become comfortable using the Python documentation, you will be able to look up other ones when you need them.

Dan Olson
Dan Olson
4,875 Points

This one seems really obvious, but actually the first step wasn't explained, so it tripped me up too. I thought I would post this for others with the same question. (While the answer above is correct, I felt like it was too generic. Sometimes it's the little things that cause all the confusion!)

So, why did the instructor have to write "from characters import Thief?" Where did that word "characters" come from? Why did I not have to write that phrase in the exercises in the next step?

The answers are simple. Because the instructor is running python in the shell, the class doesn't exist in the shell. It only exists in the separate file. To use the class in the shell, it has to be imported. To do that he calls the class referencing the file where it's written. What's the name of the file? characters.py! Thus... "from characters import Thief." Now we can use the class in the shell. Duh. So simple.