Placeholder Image

Subtitles section Play video

  • what is going on.

  • Everybody welcome to Part 15 the final part to the Python.

  • Three basics.

  • Siri's and then you guys were getting kicked out.

  • There are just a few more things I want to show you guys, Uh, let's go ahead and get into it.

  • So the first thing I wanna talk about his packages with Python so you know the reason why Python?

  • It's such a popular language in such a powerful language.

  • Yes, it's easy to write.

  • Yes, it's fast, too, right?

  • Yes, it's easy to read all those things, but there are other languages that meet those criteria, like R and Julia and Ruby and other ones that I can't think of right off the top of my head.

  • But there's lots of other high level languages, like pythons high level because it's very far from the hardware level, as opposed to like.

  • C++ is a lower level language but still classified as kind of a high level language.

  • Anyways, there are lots of languages like python, but why is Python the best?

  • In my opinion, it has to do with the community around Python and not just cause we're all really good people were fun to hang out with.

  • Um, it's It's because of the third party packages that are available with Python.

  • That's what makes Python so powerful because actually, what truly powers Python?

  • It's C.

  • So So Python is really just kind of like it's like this constant ap I slash rapper around another wise, much faster language.

  • So what makes Python the greatest is the packages, so that the main thing I want to talk about here is using third party packages with Python.

  • So there's a bunch of different ways you're gonna get third party package is the main ways with pip, which I'm gonna show you.

  • But I also want to talk about some of the other ways and what even is a package.

  • So you just understand that these things are not magical or anything.

  • So the first thing I'm gonna do is bop into tutorials, and I'm gonna copy testing grounds.

  • We'll go into testing grounds as well, and I'm also gonna add it testing grounds here, to be example mod.

  • So that's just example mon dot pie.

  • Great.

  • So the first thing I want to talk about is, I guess, let's talk about one of the more basic ways.

  • You might install a package so most of time, it'll be via pip.

  • Sometimes it'll be a package that you can only find on get hub, but you still want to install it.

  • If the package comes with a set up dot pie file, you can just use the following so we can go to our command, prompt and then using python and whether the version you want pie 37 In our case, if it had, you would.

  • First of all, you need to be in the directory that has set up a pie, but then you would just run pie 37 set up dot pie install.

  • That's it.

  • Um, so So you would do that if it had a build.

  • You might also build, but it would be in instructions.

  • Most of the time.

  • It's just set up a pint.

  • Install.

  • Very simple.

  • So, um, so that's one way you would do it, but that's kind of a dying method.

  • Now that's one of the older ways used to install packages.

  • We don't really do it that much anymore, but sometimes there will be a thing on Get up that doesn't have ah python package index thing for whatever reason, so you can use this.

  • Um, but let's also talk about what even is a package.

  • Like what?

  • It's a library with a thing we can import because we have made these imports.

  • But we don't maybe understand how that works.

  • So the first thing I want to do is, in example, mod dot pie.

  • Let's just define do a thing.

  • It's gonna pass nothing.

  • And for now, we'll just have a print doing something really cool.

  • And that's all our example Mom's gonna do just a simple function just to give you an idea.

  • So then now coming over and keep in mind.

  • Example.

  • Mine is in the same directory as our testing grounds is so now, coming into testing grounds, we can import example Mont Import.

  • Example.

  • Mod.

  • And when you import something's gonna look for in three places, it's gonna look locally.

  • It's gonna look in your standard library location, and then it's gonna look in your eye.

  • Third party library location, Which is you.

  • It's in your python insulation than lib.

  • Then sight dash packages should be in there.

  • I might have that wrong pressure.

  • That's the path, though, anyway.

  • it's gonna look at all those locations, and this is why it's really important to because it looks locally.

  • It's important to never name your script that the name of the package you intend to use because you'll wind up just importing the script that you're writing.

  • Then you're gonna get some weird attributes.

  • Air.

  • I see it all the time.

  • This is probably the number one error that people asking comment sections on tutorials of mine.

  • They're like, Hey, I'm getting this weird error.

  • I'm like you named your script, The thing you're trying to import and you're importing yourself.

  • Stop it.

  • So anyways, watch out for that.

  • So, import example.

  • Mod.

  • So this imports this script, right?

  • So it's gonna look when it goes to import any directory or file named example, Mon.

  • Not any file.

  • It would just be a dot pie file.

  • So we've imported example mind or a dot P y Z.

  • Anyway, we've imported example mod, uh, let's go ahead and show how we might use it.

  • So you could use example mod dot What does it do?

  • A thing I think it was.

  • Do a thing.

  • Yeah, so we can run this and we can see.

  • Cool.

  • It just did something really cool.

  • We just imported it.

  • Nothing you do is import something from, ah, file.

  • So we could instead say from example, mod import, do a thing.

  • And then we don't need example, mont dot We just use the function.

  • Boom.

  • Um, same thing again.

  • Pretty cool.

  • Now, the other thing we can do is we can do, um, let's just make a new function here.

  • Second function define do do another thing and print, uh, do some some other cool thing Say that import do a thing.

  • And then what was it?

  • Do another thing s Oh, Then you could do a comma.

  • Do another thing we can say that.

  • And then let's copy this.

  • Come down here, paste.

  • Run it again and you can see Okay, we've imported these two things from example mod.

  • Now, another thing that you can dio is if you were gonna import a lot of functions, you might use a wild card like this.

  • So this is just almost like a regular expression for everything or a wild card that just means all the things So we could do that and we can run this but this makes your code pretty hard to read.

  • First of all, because that someone's reading your code and, like, do a thing.

  • They're curious what is do a thing.

  • They're gonna have to do some digging to figure out.

  • Where the heck did that come from?

  • Because, like, say, like in the case where we have only one import the wild card.

  • Okay, we know where that came from.

  • Probably first, though, we're gonna have to comb through the entire script that, you know, maybe it's thousands of lines.

  • See, if this function has been to find in here and then we'll have to go to the import.

  • Be like, Okay, It's an example.

  • Mod somewhere, you know.

  • But the worst thing would be like, What if What if you just continued using these wild card imports like, you know, inside of example, MoD.

  • You've got some wild card import.

  • It just makes it really annoying toe read someone's code that's used these.

  • So my suggestion to you is don't do it, but sometimes people will do it.

  • And so anyway, just No, that's the thing.

  • The other thing you could dio is, um, you can import a thing as a thing.

  • So not only could we have imported example mod is a thing we could also import, do a thing as dat short for do a thing so that we can come down here and we can actually just run dat.

  • We've basically kind of, you know, renamed the function and we run that again.

  • So So that's pretty cool as well.

  • Also, let's say you've got a lot of scripts in a thing that we would more likely call a package.

  • Like in this case, it's just a single script that we're gonna import.

  • I wouldn't call that a library or a package.

  • Well, you would call a package is like a group, a cluster of scripts.

  • So let's make a new folder and I'm gonna call it a mod dir.

  • And then I'm gonna take example mod here, and I'm just gonna put it into my dirt.

  • So then you might also do something like this.

  • So from mod er dot example mod import, do a thing print or no, we don't have to print that.

  • We just run, do saying boom.

  • Okay.

  • And so that's how you could have, like a group of scripts inside of a directory and reference one of them specifically.

  • Okay, I think that pretty much covers.

  • Ah, the mean the extreme basics of what a module is.

  • It's not that complex.

  • In most cases, it's just a python file.

  • In some cases, it does get a little more complex because it's really a python wrapper around C code s so it does get a little more challenging and especially historically, it was very challenging to work with Windows in this way.

  • But lately, all the sea wrapped packages that I've seen installed very easily with what's called Pip.

  • Now Pip stands for Lissy.

  • Pip installs packages.

  • So it's one of those like recursive acronyms like PHP or something silly like that.

  • Um, and so of the Python Package index is what Pip kind of communicates with.

  • So here you can search for projects and stuff like that to figure out what you want to install, and then you really just install it with Pip.

  • Now the thing to keep in mind now that you've learned what a package actually is, it's just python code.

  • So remember, it's easy to forget.

  • What are you doing when you install a package?

  • Because it's a Z Z is going to your command line, and we did it with, like, numb pie in one of the videos, I think was the last one.

  • Pip installed, numb pie.

  • You know, you just run that thing and you don't really think much about it.

  • But the thing is, you're you're plausibly bringing in a lot of code.

  • And then when you use numb pie when you import numb by whatever is in like a main lube or are not a main lou but a main function to run or when you initialize that thing that you've installed, you're pleasantly running a lot of code.

  • And it's in a way that a firewall is less likely to catch because it doesn't look a suspect as you running some crazy dot e x e file.

  • So pay attention and think about what you're doing here, because when you're installing a package from Pip, you are trusting someone else's code.

  • You're just taking it unless you're reading every line, which I know you're not like.

  • You have to understand what you're actually doing.

  • And because of this, I would really watch out because, like, for example, really recently, um, let's say a python package python package.

  • Oh, my gosh.

  • Package index mulish.

  • Yes, just the other day.

  • Here we go.

  • Six days ago on this website