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?
Python's ImportError ( ModuleNotFoundError ) indicates that you tried to import a module that Python doesn't find. It can usually be eliminated by adding a file named __init__.py to the directory and then adding this directory to $PYTHONPATH .
Just do type(what_ever)==type(None) for this one instance.
In Python, ImportError occurs when the Python program tries to import module which does not exist in the private table. This exception can be avoided using exception handling using try and except blocks. We also saw examples of how the ImportError occurs and how it is handled.
How can you tell if a object is a NoneType? Use the is operator to check for NoneType With the is operator, use the syntax object is None to return True if object has type NoneType and False otherwise.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With