Import my custom util file first or last?

Python 3.12 on Windows 10 Pro.

I have a bunch of custom routines in a custom library called “ggutil.py”. Should I import this “ggutil” before all other imports (first) or after all other imports (last)?

I do it last at this point.

import inspect
import os
from os.path import exists
import re
import sys
import glob # To get list of files.
import camelot
import pandas as pd
import xlsxwriter

from ggutil import * # My Custom utils. 

Why? Are there any errors? If not then it’s completely up to you where you want to import something and where not :slightly_smiling_face:.
And I think it’s important that imports from the same module are written after each other, not sprinkled in with other imports from different modules.

In terms of style, there is this guide.
Two reasons why putting them last can be convenient:

  1. If, as in the style guide, you follow an expected order, like from more general imports to more specific, and alphabetized within each group, then it is easier for you to search a long list of imports.
  2. If your modules deliberately re-define some names from more general modules, then the effects remain in your script. For example, if in ggutil you happened to modify exists for some reason, then in your script you will have that modification available. If the re-definition happened accidentally, well …, I guess having your own imports in an expected order (at the end) you might be able to see the unintended effects in your script. (Note that the style guide also recommends avoiding import *).

I also do it last. Unless there are some special ordering issues (which
broadly there never are), this is a matter of style.

I put:

  • stdlib modules first
  • third party modules next
  • my modules
  • the local packages internal modules

with the groups separated by a blank line, and lexically ordered within
the groups.

Here’s a big example from my cs.ebooks.pdf module with several stdlib
modules, some third party modules, my personal modules, and the local
.cbz module i.e. since this example is from cs.ebooks.pdf, the
.cbz loads cs.ebooks.cbz.

 import binascii
 from collections import defaultdict
 from dataclasses import dataclass, field
 from functools import cache, cached_property, partial
 from getopt import GetoptError
 from io import BytesIO
 from itertools import chain
 from math import floor
 from mmap import mmap, MAP_PRIVATE, PROT_READ
 import os
 from os.path import (
     basename, exists as existspath, join as joinpath, splitext
 )
 from pathlib import Path
 from pprint import pprint
 import re
 import sys
 from tempfile import NamedTemporaryFile
 from typing import Any, Callable, List, Mapping, Optional, Tuple, Union
 from zipfile import ZipFile, ZIP_STORED
 import zlib

 from icontract import ensure, require
 from PIL import Image
 from typeguard import typechecked

 from cs.binary import AbstractBinary
 from cs.buffer import CornuCopyBuffer
 from cs.cmdutils import BaseCommand
 from cs.deco import promote
 from cs.lex import r
 from cs.logutils import debug, error, warning
 from cs.pfx import pfx, Pfx, pfx_call, pfx_method
 from cs.queues import IterableQueue
 from cs.resources import RunState, uses_runstate
 from cs.threads import bg

 from .cbz import make_cbz
1 Like