Skip to content

Recipe ERROR NoneType has no attribute

Steve (Gadget) Barnes edited this page Apr 26, 2023 · 1 revision

It is not too uncommon to have a perfectly running from source code program that when built into a Windowed executable throws an Exception such as:

Exception Attribute Error: `NoneType` object has no attribute ....

This is caused when the underlying code, (sometimes in a library), is trying to make use of an attribute of either stdout or stderr such as isatty - the problem in this case is that on some versions of pyinstaller both stdout and stderr are set to None which has very few attributes or methods.

Of course updating pyInstaller may well fix this for you but the may be reasons why you are constrained to your current version.

One possible solution is to change where this I/O redirection occurs to and this can be fixed thanks to the lazy importing of sub-modules with the following code in your main or entry point module.

import sys
import os
# Do whatever other imports that you need

# Fix for stdout/stderr is None
if sys.stdout is None:
    sys.stdout = os.devnull
if sys.stderr is None:
    sys.stderr = os.devnull

# The rest of your code follows.

Since python usually only performs each import exactly once overriding sys.stdout & sys.stderr in your main or entry point before any other code will also override them for any libraries or other modules that you have loaded. _The exception is multiprocessing systems where you need to ensure that the entry point(s) all have the above code or similar.