Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 551
deltanovember Avatar asked Apr 05 '13 22:04

deltanovember


People also ask

How do you fix an import error in Python?

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 .

How do you access NoneType in Python?

Just do type(what_ever)==type(None) for this one instance.

What is import error in Python?

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 do you test for NoneType?

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.


1 Answers

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.

like image 86
poke Avatar answered Oct 19 '22 03:10

poke