pythonpython-3.ximporterrorporting

Why am I getting an error message in Python 'cannot import name NoneType'?


I'm trying to convert some code from 2 to 3 and the following simple script

import types
from types import NoneType

Results in

ImportError: cannot import name NoneType

How can I convert the above from 2 to 3?


Solution

  • There is no longer a NoneType reference in the types modules. You should just check for identity with None directly, i.e. obj is None. An alternative way, if you really need the NoneType, would be to get it using:

    NoneType = type(None)
    

    This is actually the exact same way types.NoneType was previously defined, before it was removed on November 28th, 2007.

    As a side note, you do not need to import a module to be able to use the from .. import syntax, so you can drop your import types line if you don’t use the module reference anywhere else.