Write Pythonic Code Like a Seasoned Developer Transcripts
Chapter: Modules and Packages
Lecture: Pythonic import statements

Login or purchase this course to watch this video and the rest of the course contents.
0:01 Let's talk about import statements.
0:02 Over here we have a variety of things being used
0:05 that should come out of modules that are mostly in the standard library
0:09 but some of them we build ourselves.
0:11 So you can see we're using the system module,
0:13 we say "sys.versioninfo.major" so this should print out "3"
0:17 the way we have our system setup right now,
0:19 of course you can see there is an error here
0:21 and we should come over here and say "import sys".
0:24 So this is one way we can do this,
0:27 and this is certainly considered Pythonic, it's using namespaces, right,
0:32 so "sys.", we know where this version info is coming from,
0:36 and remember, namespaces, those are one honking great idea,
0:41 let's do more of those.
0:42 OK, cool, so we are doing more of those here, now sometimes,
0:46 you don't want to have to say the namespace or the module name here,
0:49 and especially this really long like "SQLAlchemy.orm.ext.declaration.",
0:56 this is maybe a little bit long, but sys, sys is great.
1:00 However, if you would like to use a type over here we can say "import os"
1:06 and we could come over here and we could say "os.path",
1:08 but we'd rather just say path, so instead we'll say "from os import path".
1:14 We can do it like this and this is also considered very Pythonic,
1:18 there is a shortcut way, we could get hold of, here we are using the stats module,
1:24 statistics technically, we'll rename it, so we could get hold of median,
1:28 the mode, the mean and all those things,
1:30 if we said "from statistics import", rather than naming them,
1:36 the correct way would be to say median mean and so on,
1:40 name them, but we could say, let’s leave this here and I'll comment it out,
1:45 we could just say you know, I'll just use everything, that's cool thanks.
1:48 Well, you can see all these 3 lit up
1:51 this is considered to be not Pythonic at all, let me show you why.
1:55 So we have this, suppose we want to use median and mean from the statistics module,
1:59 and let me go ahead and import this as well,
2:01 so we'll say "import statistics as stats",
2:06 so here if we want and if we had something really long,
2:08 like I said "SQLAlchemy.orm.declarative. da da da", we could rename it,
2:12 so here we could say "import statistics as stats",
2:15 and then just say "stats.", so kind of taking over the namespace, to greatly simplify it.
2:20 Suppose mode here is not the statistics mode,
2:23 but we want to set the mode of operation of our program,
2:26 so we have kind of a crazily named file here, beware_the_trade_imbalance support file
2:34 and in there, there is a mode function, you can see "def mode", it prints,
2:39 that really didn't do anything,
2:40 but under one condition it prints "Mode set to DEFAULT" or "Mode set to ADVANCED".
2:44 So when we say "mode" down here, this is actually the mode we want to use,
2:47 so I can come over here and say OK, cool,
2:49 so I'll say "from..." we want to import mode.
2:55 Now, PyCharm just because of this weird naming convention I had here,
2:59 thinks this is a private thing,
3:02 so let me just tell it: "Don't make this look like an error for us."
3:07 Because normally that would maybe mean "protected" but it doesn't in this case.
3:11 So, now all of our code should run, we print out the major version, the current path,
3:17 the median of those numbers, again, that should be the same median, then the mean,
3:22 so the median, median, mean, and just like we would expect
3:26 we have our mode set to advanced.
3:29 So our mode is set to advanced.
3:31 The None is coming from, let me actually, just this doesn't return the value
3:34 in that case we wrote.
3:35 OK, so that's the way it worked,
3:37 but suppose instead of doing this Pythonic style of importing without the namespace,
3:41 I said you know, I just don't want to worry about it, let me,
3:44 I want to have a bunch, let's just get them all, so we can go like this.
3:48 However, even though I was not using the mode from statistics,
3:55 this imports mode from statistics, flat out, and all the other ones,
3:59 I am not sure how many are in there but many of them.
4:01 Now notice, PyCharm knows something is amiss here.
4:05 This has gone gray and says, "you are not really using this mode",
4:08 that should be a warning that something is wrong,
4:11 so if I remember this said "mode set to ADVANCED", now If I run it,
4:15 mode is seven, what happened?
4:18 We imported mode and then we reimported mode,
4:21 which replaced its definition in this module.
4:24 So mode is gone. All right, we never intended to use mode from statistics,
4:28 we just wanted to be lazy and not import those two separately,
4:32 this is bad, don't do this.
4:35 Notice when I comment this out, this gets enabled again
4:38 and let's do one more thing, notice how this got some errors
4:42 and these are undefined, and I did type it up here but let me remove it for a minute
4:46 and just show you the PyCharm can fix this,
4:48 so if I go over here I can hit Alt+Enter and it will say,
4:50 now because I have the stats up here already it knows,
4:53 it's trying to help too much, so if I come over here and I hit Alt+Enter,
4:56 it'll say "import statistics.median" and I hit this, great,
5:01 and again Alt+Enter, you can see,
5:03 it's doing the Pythonic way of doing the import here for us, perfect.
5:07 And of course, let's put our stats back because we were confusing PyCharm there.
5:11 Let's see this in a graphic.
5:14 So namespaces are great, "import sys", that was cool,
5:17 importing bare items by name, that's fine, "from os import path",
5:23 we could rename or shorten our namespaces, "import statistics as stats",
5:28 maybe it doesn't make sense in this case,
5:30 but I will give you some examples where it does,
5:32 and we also saw that we have to be very careful
5:35 with wild card imports, "import *", because the two lines above,
5:39 we are trying to get mode from our custom package
5:42 and mean and median from stats
5:44 and if we wanted to get lazy and say "forget mean, median,
5:47 and we should write *", we would blast away our custom packages mode definition.