Write Pythonic Code Like a Seasoned Developer Transcripts
Chapter: Style guidance from PEP 8
Lecture: Import statements

Login or purchase this course to watch this video and the rest of the course contents.
0:01 The first PEP 8 recommendation that we are going to look at is around importing modules and packages.
0:06 So let's switch over here to PyCharm and have a look at some not-so-great code. You can see on line seven here, we are importing collections,
0:13 the recommendation from PEP 8 is that all module level imports should go at the top, unless there is some sort of function level import
0:20 you are doing kind of unusual, conditional things. And notice there is a little squiggle under here,
0:25 and that's actually PyCharm saying "here is a PEP 8 violation, a module level import is not at the top of the file."
0:30 So we can take this and put it at the top of the file, now everything is happy. So let's look at few other things we can do,
0:38 we could say "from os import change directory, change flags and change owner." This is a good way to import a bunch of items
0:48 from os and not have to state their name, we could do this a different way, using what's called a wildcard import, we could say from os import *,
0:56 now that would import the three listed above, but it would also import every symbol defined in os. Now what's wrong with this?
1:04 Imagine up here I imported another module, from my module import path, so if I write this code,
1:11 line four and line six, path is not going to be what you think it is, because we are importing path here but then we are importing every symbol from os
1:19 using what is called the wildcard import, and os also has a path so it is going to overwrite the definition of path here.
1:26 So it's why it's always recommended to use this style of importing. You may wonder why this is gray in PyCharm,
1:32 PyCharm is just trying to help us out saying "here are some unused imports you can actually remove", but if I write something like "c = chdir",
1:39 then that part will go away. But of course, because we are importing the same thing twice basically it's also saying, I'll move that.
1:46 Here we go, so now it says "you are using change dir, change owner but not change flags", down below it's using "change owner".
1:53 All right, now "change flags", if I do something with that, it also lights up. All right, so there was never a problem with that import,
2:00 that was just PyCharm trying to tell us "hey, look out, you are actually not using that import."
2:04 Another mistake that people make is they might say something like this, "import collections, os" and let's say "multiprocessing",
2:11 they may put multiple imports on a single line. That also works just fine, however, again, PEP 8 recommends that you put one import per line
2:20 so it's very clear line by line what you depend on here, so we could fix this by saying, let me just put a "no:", something like that,
2:29 and we could of course fix this, like so, "import os", "import multiprocessing". So this would be the recommended way to write what was on line four.
2:40 So let's look at that import guidance a little more clearly. Here we have at the top two bad styles of imports,
2:48 first line: "import sys, os, multiprocessing", on a single line; PEP 8 says "do not import multiple modules on a single line",
2:56 and avoid "from module import star", these wildcard imports because you may accidentally, unknowingly overwrite other imports.
3:04 So we have our better set of imports, "import sys, import os, import multiprocessing",
3:10 and these of course are going to allow us to use "module name.symbol" name so "os.path" for example, in this sort of namespace style,
3:18 and that's really nice to know where the particular symbol that you are working with, like "path", where it came from.
3:24 If you don't want to use that namespace style, you can use the final import we have here, "from os import path, change mod and change owner".
3:32 PEP 8 also have some guidance on the order and grouping of your imports. It says the standard library imports should go at the top,
3:40 related third party imports should go in a little section below that and then finally, local app other models within your own code should be put last.
3:49 Of course, all three of those go at the top of the file. Another thing that's nice about PyCharm - it does this for you automatically
3:57 if you hit Command+Alt+L for reformat file.


Talk Python's Mastodon Michael Kennedy's Mastodon