6) d. Imports

PEP 8 also includes a few best practices regarding imports.

First of all you should never import multiple packages on the same line.

1
2
3
4
5
6
7
# Don't do this.
import math, socket, random

# Do this instead
import math
import socket
import random

However if you want to import multiple items from one package you can list them in one line like in this example

1
2
from subprocess import Popen, PIPE, DEVNULL
from typing import Optional, Union, List, Dict

You should also avoid the following pattern as much as possible.

1
2
3
4
5
6
7
8
9
# Don't do this. It becomes messy very quickly.
from tkinter import *

button = Button()

# Do this instead. This way you know exactly which package items belong to.
import tkinter

button = tkinter.Button()