Python Fundamentals Python Fundamentals

Importing Modules, & the main Method

Check out a free preview of the full Python Fundamentals course:
The "Importing Modules, & the main Method" Lesson is part of the full, Python Fundamentals course featured in this preview video. Here's what you'd learn in this lesson:

Nina demonstrates how to import files into another file, how to create a module using dunder name, and briefly gives an example of how to include an error message in your log.

Get Unlimited Access Now

Transcript from the "Importing Modules, & the main Method" Lesson

[00:00:00]
>> Nina Zakharenko: I'm gonna make a new file, and I'm gonna save this one as nam_lib.py, and I want it to be a library function that does some useful things with my name. So i'm gonna say upper_case, I'm gonna make a new function, call it upper_case_name, and it's gonna take a name.

[00:00:21] And it's gonna return name.upper, so the uppercase version. And now I'm just gonna put some code in here, I'm gonna say my name is "Nina". And I'm gonna get the uppercase version of my name,
>> Nina Zakharenko: And then I'm gonna print out that value.
>> Nina Zakharenko: Okay, if I run this file, it'll print out uppercase name is Nina,

[00:00:59]
>> Nina Zakharenko: Right, okay, pretty useful. Let's say that I'm a developer or even I want to reuse my code, and I see this awesome new library with my upper case method. And I'm like, great, I want to use this in my code, too. So I'm going to make a new file and I'm gonna call it,

[00:01:25]
>> Nina Zakharenko: Other_program.py. And in my other program, I'm going to import name_lib. That's the name of this Python file, that's in the same directory as mine. And I'm gonna say okay, well, my_name = "Fred". And the upper of my name is gonna be in the name_lib using that upper_case_name function, and then I'm gonna pass my own name into it.

[00:02:06] And then I'm gonna print it out, too, right. Okay, so in my own code, upper name is upper_name. We're gonna see something unexpected happen here. So if I run this code, oops, I feel like I'm missing.
>> Speaker 2: I think you overwrote your function with the string.
>> Nina Zakharenko: Where?

[00:02:33]
>> Speaker 2: In name_lib.
>> Nina Zakharenko: I totally did, thank you for catching that.
>> Nina Zakharenko: So I'm gonna call this one name_upper, and just run that again. It says my upper case name is NINA, okay. So in my other program, now I'm Fred, using this library. I run it, and okay, it says in my own code, the upper name is Fred.

[00:03:04] But it's also printing out, upper case name is Nina, that's totally not what we want. That's because when Python doesn't import, it just kind of follows all the stuff, step by step. There's a conditional that we have to set, and it's pretty kind of recommended. If you want your code to be reusable and there's any code in your library that's kind of just for you, you wanna put it in a main method.

[00:03:41]
>> Nina Zakharenko: And to understand how the main method works, I'm gonna print out the value of __name.
>> Nina Zakharenko: Underscore underscore in Python, or double underscore, sometimes called dunder, so just so you know what that means. I'm gonna run this. So when I run this file directly, dunder name is equal to __name_.

[00:04:15] If I run my other program now, we'll see all this stuff is printed out again. But now when I'm used as a library, my dunder name is the name of the file, the name of the import. So if we only want our special code to run when we're running this file directly, we're gonna want to check, if dunder name is equal to a particular value, then run this code.

[00:04:52] What are we checking for?
>> Speaker 2: If it's equal to name?
>> Nina Zakharenko: If it's equal dunder name.
>> Speaker 2: Underscore, underscore
>> Nina Zakharenko: Mm-hm.
>> Nina Zakharenko: So for my if statement, I have to indent. In VS code, if you select all the lines, you can just hit Tab once. Okay, now if I run my library, I see uppercase name is NINA, but if I go back to my other program, and I run it, I just see, in my own code, my upper name is FRED.

[00:05:30] Does that make sense?
>> Nina Zakharenko: Okay.
>> Nina Zakharenko: When we're working with files like this, an uncaught exception is going to exit out of our program. So I'm gonna make a new file called exceptions.py. And let's do something that we know is not allowed. I'm gonna call int with a string.

[00:06:04] It's gonna throw an error, right. This will throw an error. And then I'm gonna print out, this is the end of my program. So running this in the terminal, I see my value error printed out. But we don't ever get to the end of the program, it just exits.

[00:06:37]
>> Nina Zakharenko: In order to get around this, you're gonna want to use a try and except block. So you put the code that you're kind of, you're not sure, maybe it'll throw an error maybe it won't. You put it in a try block, and then right underneath the try, you put an accept.

[00:06:56] In the except, you need to put the exact exception class that you want to catch. And luckily, we have that right here in the trace back. So I'm just gonna copy that exact thing. And then I'll put a colon, and then print something out.
>> Nina Zakharenko: When I run this, now I reach the end of my program.

[00:07:26] And I also print out the code that's in the except block. You can use as to give this exception a variable name, that you can then use within that except code statement. So now I can print out e, and it'll give me more information right here.