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

Unlock This Lesson

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

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set the default subtitles language in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

What Happens When You Import a Module

Give Feedback

00:00 In this part, Martin will show you what happens when you import a module. He will also explain the __name__ == "__main__" statement that you already briefly saw in the last part.

00:13 And he will also mention tests a little bit. Although he won’t go into detail in this part, we will get to this in one of the next ones. For now, it’s mostly about what happens when you import a module.

00:28 So let’s make a new file in here. Call it—well, that can be the test file, right? I can write some tests for this as well. Yeah, you were mentioning tests when you were thinking of putting it into a function.

00:39 You were mentioning it would be easier to test, so that’s actually a good step. So now you’re creating a new file, and now you want to import the double_characters() function from double. Now we’re still showing about this __main__, why it’s helpful.

00:56 So if I say from double import double_characters, it’s the function. And then I’m just thinking, I want to call it here. I’m going to say double_characters() and pass in "Tree" or something.

01:10 And now if I go ahead and run this,

01:14 you might expect that you get "Tree" doubled printed out, and you will, but there’s also an additional thing. You can pause for a second and think about what’s going to happen. So if I run this, I get both Philipp’s name and then the input that I actually put in here printed out.

01:29 And the reason is just because I have this print statement in this file and what Python does when you import a module is it executes the script.

01:38 Because it needs to define the functions in there and it assigns "Philipp" to word, it also executes this call to print(). Yeah.

01:46 And if you’d be wanting to use double_characters() function in any other script, you obviously don’t want it to print Philipp’s name, always unless—I don’t know, if you’re maybe…

01:59 Not for everybody involved. So you don’t want that, but at the same time, you still want to be able maybe to test the file itself, to just take a look whether it’s working.

02:09 And one thing that is often done in Python scripts is that you use this, make use of the namespace, which when you run a script, then it runs in this namespace called main.

02:23 And that means that when you run the script directly, but not import it, then this if statement is going to be true, and then the print() will happen.

02:32 And if you import it, then it won’t because then it’s running in the namespace of the file. So the namespace is going to be double, and here, the namespace is going to be main if you run it directly. So I would move this

02:46 in here as well, because we don’t need to define this word either. And now if I run test_double, I’m only going to get the TTrreeee.

02:56 But at the same time, if I run the file directly, I get this if that name is true, and then the indented code here executes. I mean, the point of this is just that you can still see some output from running the script directly, but it doesn’t interfere with the possibility for you to import the code somewhere else. Yeah, that was a really nice explanation in that regard that basically you would be using __name__ == "__main__", and dunder is like a shorthand term for double underscore (__), right here.

03:31 You can import the file without running the code, but if you actually want to run the code of the script, then everything that’s inside of this if statement is executed when you run the file.

03:42 So basically you now have this double.py file, which is good for importing, but also good for running on its own. Cool. So let’s write some tests.

03:56 As you saw, the if __name__ == "__main__" statement is really handy. The special variable __name__ has the value __name__ inside scripts, but it gets the name of the module inside imported modules.

04:11 By using this pattern in a Python file, you create code that serves two purposes that are isolated from each other. It’s executable as a script, and it’s importable as a module.

04:24 To show this behavior, Martin also created a file with a test prefix.

04:30 In the next part, he will follow up with this naming and create some tests.

Become a Member to join the conversation.