Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Importing Objects From a Module

00:00 Instead of importing the entire namespace from a module, you can import only a specific name. To do this, you use an import statement that says, from <module> import <some_name>.

00:14 Go back to the main file in your IDLE editor and now bring the file into the state that it was before. So in line one, you change from adder as a to import adder,

00:31 and then you have to change in line 3 value = a.add() to adder.add(). And in line 5, you need to change a.double() to adder.double() Save the file and run it to check if everything works.

00:50 And you see 4 and the 8 are printed. So your file works perfectly. And now let’s try something else. Still in main.py, change the import statement to the following: from adder import add, then save the file.

01:09 And before running it, take a moment and think what will happen

01:16 this time. Again, a NameError exception is raised just like before. When you change the import statement, you get a traceback that tells you that the name adder is undefined.

01:27 The reason for this is that only the name add is imported from adder.py and is placed in the main.py module’s namespace. The adder namespace from before doesn’t exist anymore.

01:40 That means you can use the add() function without having to type adder.add(), or, let me rephrase that. You actually must use the add() function without putting adder and the dot before the function name. So let’s update the code accordingly.

01:56 Replace adder.add() and adder.double() in main.py with add() and double().

02:05 Now save the file. And again, take a moment to think what will happen. Well, by now you should know that I use the name errors as my teaching companion.

02:14 So when you run the code, another NameError is raised, but this time the NameError is a bit different than before. Now it says NameError: name 'double' is not defined.

02:27 Okay. So this time the NameError tells you that the name double isn’t defined. And if you have a look at the import statement, then you can verify that yeah, indeed we only imported add but not double.

02:41 So when you write import statements like this, you gain control over which names from the module you want to import. So with from adder import add, only the add name was imported from the adder module. To also import the double name, you put a comma after it and then write double.

02:59 So the line now is from adder import add, double. Save and run the module. Now the module runs without producing a NameError, and 4 and the 8 are printed in the interactive window. Perfect. Now that the code works, it’s time to show another slide.

Become a Member to join the conversation.