 Hey everybody, this is Brian. Welcome to the 9th Python tutorial. Boy, we're just flying through these little guys. We're going to make a new Python file and call it video9. All right. And today, we're going to be working with modules and packages. So what is a module? Module is anything that ends in .py. We've been making these, you've made nine of them if you've been following along my videos. So why would you make a module? Well, it's pretty self-contained like here's video5. You know, there's some code in there. You know, you don't want to make one giant file. You want to break things into different files. So let's actually just make another file. Let's call this my module. Why not? And here's my module right here. I want to say class. Let's call this person. Here it's off of object. And we're going to make a definition here. Actually, no, we're going to make a variable name equal. And then we're going to make a def. Let's call it and say hello. You know, you've done this before. Hello, my name is, and then %s. I'm going to say %selfname. So this is nothing new, but why am I doing this? What's this have to do with the discussion? Well, we've made a module like we've done in the past. Now we want to access this class and use it in a different module. We're going to say from my module, import, person. Now we can actually use the person class. We'll say, I need a good name here. Let's call it person1 equal person. Did I not call that person1? There we go. Person1, name equal Brian. Really help if I had quotes in here. That was embarrassing. Can't even spell my own name. All right. Person1, say hello. Now when we go to run this, hello, my name is Brian. So we have all the functionality of the person class which exists in the my module file in the same directory. But we don't have to have all that code in here. All of it's included right here. It's basically the exact same thing as taking this and just copying and pasting it right there without having it cluttering up your file. But as you might have guessed, when you go to deploy this file, you also have to include anything that you're using. It's called a dependency. So that's an example of a module and why you would want to use a module. Now let's talk about packages. Package is a little bit different. A package is a directory, and we're going to actually make one here. Python package. And let's call this the ever descriptive my package. Because I like working with my package. Now those of you in the back row that are snickering right now, pay attention, this is serious. All right, so my package is just a directory with a special file in it, a special module called underscore underscore init underscore underscore dot py. When you open that it just says author and then whatever because you know PyCharm put that in there. And in here, we're not going to really discuss too much in this video. But in here you can put special commands that will tell what to include and what not to include, what's public, what's private, and you know all sorts of neat stuff. So if your structure of your program starts getting really unruly, consider making a package. And let's actually add another file in here. And let's call this animal. You know, I'm sick of working with animals. Let's call it a car. All right, so we've got our car in here. And we're going to say def, well not def, geez. Class, car. I am going to actually cheat a little bit here. I'm going to just take this thing. Nice sound effect there, right? And we'll call this car. Let's call this set speed. Going this fast. My eyes got a little blurry for a minute. I have eye surgery. If you've been watching, you already know that sometimes my eyes get blurry. Kind of bugs me a little bit. Don't really need a name for a car. So nothing new there. I mean we just got a package with a module inside of it. And we're just going to say, let me look at my notes super quick, from my package, car, import, car. So what you can actually do now is you can say from a specific package, a specific module, import a specific class. And then we can say, my car, equal car. My car, set speed. And we're going to go 100. Why not? Let's run this and see what happens. And hello, my name is Brian, and going this fast, 100. Now you might be asking, how can I actually import from this module? Well, I don't really know the name. Well, I don't really care what the name is. You know, I want to just import everything in there. So if there's multiple definitions in here. So like we can say, let's actually make a truck. We're going to call this truck, truck, there we go, truck, set speed. So now we've got a truck and a car. And I'm not really going to check these out too much just because, you know, it would be, whoopsie, a waste of time there. What have I done? Well, I got a little too click happy there. All right, so now we've imported that. We can also say, my truck, equal truck. Notice how it's just automatically importing it. We didn't have to specifically say truck. And then we can say, my truck, set speed. And we're going to say 90. Why not? Let's run this little guy. Hmm. Missing one required position argument. What did I do wrong here? Okay, now I feel very stupid. I just simply forgot the parentheses. All right, so there we go. So the car is going 100, truck is going 90. So you can see how that works. Pretty interesting, huh? Now, something that's really going to kind of blow your mind here is, why am I showing you these other than you can just, you know, organize your code? Well, you need to get used to importing from other sources, other people write modules. For example, if we click on external libraries, you'll see how there is a ton of stuff in Python 3, just expand 3.4. These are pretty much all the packages and modules that you can really access. And there is just tons and tons and tons and tons and tons of them. Whew, there's a lot of stuff in there. And to kind of help with this, I'm going to actually post a link out here. I'll put it in the top of the file for you. We can go out there and you can go to the Python Standard Library. If you're familiar with C++, you know what a Standard Library is. But if not, I'll explain it. A Standard Library is all the functionality that comes with the language. This is all built in. Remember when I said Python's motto is batteries included? This is why. I mean, it's got just a mega ton of stuff. I mean, just about anything you can imagine. For example, I mean, you know, even Gison encoding, operating system, XML, Eurowriters, input, output. It even has a graphical user interface, which we may cover in future tutorials. I mean, the list just is immense. I mean, it's actually grown very impressively. But believe it or not, Python's Standard Library is considered small compared to some languages such as Java. The Java framework is just massive. So I was going to actually do this in a different tutorial, but why not? Let's just do it in this one. I mean, we're on the subject. So we're going to import and let's say sys. This is a pretty standard package. And we're going to just say print, whoops, print sys.version. So we want to know the system version. And you can say the version is 3.40, which is the current version that Python that we're running. Pretty interesting. Now, let's say, let's cover the dir command. We haven't covered that yet. Dir will iterate over pretty much anything. So if I actually import, let's say, operating system or OS. What dir will do is it'll just print out everything that's available in there. And it's kind of long, as you can see. It just keeps going and going and going. And if you're in idle or if you're working with Python right on the command line, it's actually much easier. That's kind of what it's designed for. So let's actually run Python, say Python 3. And let's just say import OS and we're going to dir OS. And you can see that's why it's there. It's for working on the command line. Now, let's say there's something specific you want to know about. You can use what's called the help command. You say help OS.write. And it says, OK, this is the built-in help for that module. And what it'll do is it'll say write, and then you give it a file definition and data, and it'll out port the bytes written. And it says writes byte to a file descriptor. So that, if you're ever stuck working with idle, is very, very helpful. But working with something like PyCharm, it's not that helpful at all. So I just wanted you to be aware of it. That's pretty much it for this tutorial. Hope you found this educational and entertaining. Be sure to go over the Python Standard Library. Many times, if you're just kind of curious about something like ReadLine, it'll actually have, maybe that was a bad example. Nope, here it is. They'll have example code right in here. So you don't have to go hunting in forums and Google and user groups. It's right in the library. So it's a really, really awesome resource that you need to kind of go through. Make life a lot easier for you. Be sure to visit my website, voidrealms.com, for the source code for this and other tutorials. And of course, join the Voidrealms Facebook group. I think we just eclipsed 200 users. A bunch of helpful people in there.