Intermediate Python Importing Modules
Transcript from the "Importing Modules" Lesson
>> Nina Zakharenko: Now for modules and imports, Python has a pretty simple package structure. But this has changed between Python 2 and Python 3. So in Python 2, this dunder init.py file was required to be in a folder for Python to consider that folder a package module. In Python 3, it's no longer required, but it is still supported and it can be useful.
>> Nina Zakharenko: Let's make a basic function. And we're gonna make a new module.
>> Nina Zakharenko: So going back to the VS Code here,
>> Nina Zakharenko: If you don't have a PowerShell for Windows or a Bash for Mac, you might wanna create a new terminal window here.
>> Nina Zakharenko: And if you'll notice, because I have the Python extension activated, when I make a new terminal window, VS Code automatically sources the virtual environment, so handy.
[00:01:15] Okay, so I'm gonna make a new folder. And I'm gonna call it my_math_functions. And then I'm gonna cd into there.
>> Nina Zakharenko: I'm gonna make a new file and,
>> Nina Zakharenko: I'm gonna call it __init__.py.
>> Nina Zakharenko: You can also open a new window here and save it, so bbb. In this my_math_functions __init.__py, I'm gonna put this simple function, add_numbers(x, y).
[00:02:06] I want to go back up one directory to pyworkshop, because I'm gonna be importing this folder. So let's try that again. I'm gonna type python. And I'm gonna say, import my_math_functions.
>> Nina Zakharenko: Now I can call my_math_functions.add_numbers.
>> Nina Zakharenko: There we go, we can use that as a library. So generally,
>> Nina Zakharenko: If you wanted, if I had a bunch of functions in here, it's valid syntax for Python, but don't do this. I could say from my_math_functions import *. And that's gonna go and import everything from this file into my current environment. And then I can just say, add_numbers three and five.
[00:03:16] But this isn't great. Generally, when we're using functions in these types of modules, we wanna be as specific as possible and only import what we're going to use. That lets us keep our environment, also called a namespace in Python, as clean as possible, and not littered with other values that we don't care about.
[00:03:40] So if I just wanted to import add_numbers, I would say from my_math_functions import add_numbers.
>> Nina Zakharenko: And then I can say add_numbers(1, 2).
>> Nina Zakharenko: If our library has a useful name, which it should, our module, sometimes, and we plan on importing quite a few things from it. Sometimes it's okay to just say, import my_math_functions, and then call my_math_functions.add_numbers.
[00:04:31] Because that gives you some context of where that method came from. So different best practices for different scenarios. You can also use that as keyword, like we did with exceptions, to change how that import is seen in your current environment or your namespace. So there's a function called prettyprint that we haven't talked about yet, that helps you print out large dictionaries in a little bit of a cleaner way.
[00:05:13] So let's say I had a dictionary with some squares in it. The key is the number, the value is the squares. And I'm gonna say for num in range, say 30.
>> Nina Zakharenko: If I just print my dictionary, it's gonna be a little bit of a blob. I can import something called prettyprint.
[00:05:47] I'd say from prettyprint, which is the module, import prettyprint, which is the function. Sorry, it's not prettyprint, it's pprint. So I would say from pprint import pprint. And pprint stands for prettyprint. Now I can say pprint(my_dict). And look at that, it prints it out a little bit nicer.
[00:06:12] There's some new lines here, a little bit easier for us to read. But pprint is kind of a mouthful. So I could also say from pprint import pprint as pp. And now that’s considered an alias. If I didn't already import pprint, it would not be available in this context.
[00:06:35] I would just have to use pp, pp(my_dict). And that works just the same.