What is __future__ in Python used for and how/when to use it, and how it works

The __future__ module in Python allows you to enable new language features which are not compatible with the current version of Python. These features are typically included in the next version of Python, but can be used in the current version by importing the __future__ module and enabling them.

Watch a course Python - The Practical Guide

You can use __future__ by importing it and using the features you want. For example, to enable the print_function feature in Python 2, you would add the following line at the top of your script:

from __future__ import print_function

The __future__ module is intended to ease migration to future versions of Python that introduce incompatible changes to the language. These changes are usually made to improve the language, but they can also break existing code. By using __future__, you can test your code with the new feature and fix any issues before the feature becomes the default in a future version of Python.

It should be noted that __future__ is a normal module like any other. When a module is imported, its code is executed, so the statements in the __future__ module change the semantics of the code that follows the import statement.