Why does an error occur from __future__ import *?

I used the following import:

from __future__ import * 

but got this error:

 SyntaxError: future feature * is not defined (<pyshell#0>, line 1) 

What does this error mean?

+8
python python-internals
source share
2 answers

Although importing * from a future module is probably dangerous and should be avoided for the reasons John Zwink talks about, it was interesting to find out why this does not work. It works differently with the usual Python import syntax, which allows you to extract everything from a module using * .

You can see what happens by opening Lib/compiler/future.py in the Python installation directory: all import statements imported from __future__ are launched through a special parser that allows you to just try to import one of the predefined functions. From the source code of FutureParser :

 features = ("nested_scopes", "generators", "division", "absolute_import", "with_statement", "print_function", "unicode_literals") 

Basically, you were right to __future__ that importing from __future__ is a special case that is slightly different from the usual Python import process, but there are good reasons for this.

+9
source share

Importing “everything” from the future is neither desirable nor reasonable. In fact, most of the time you should not import * at all, but in the case of __future__ this is especially insidious: what functions do you intend to get? It would be very difficult to write the right program that will work with future versions of Python whose functions are not yet known.

+7
source share

All Articles