__future__ Module in Python



What is the __future__ Module?

The __future__ is a built-in Python module that is used to import features from newer versions of Python into older versions. This helps you to write code that will work the same way in both old and new versions of Python.

It is mainly used when you are migrating code from Python 2 to Python 3, or when you want to try out new features before they become standard (default) in future releases.

For example, in older Python versions, dividing two integers would return an integer. But if you import the division feature like this:

from __future__ import division

Then division will follow the newer behavior, and dividing two integers will return a float result.

Common Features Imported Using __future__ Module

Following are some of the commonly used features that can be imported using the __future__ module:

  • division: Makes the division operator / always return a float result.
  • print_function: Makes print statement behave like a function (requires parentheses).
  • unicode_literals: Makes all string literals Unicode by default.
  • absolute_import: Forces the import system to always use absolute imports.
  • generator_stop: Raises RuntimeError if a generator raises StopIteration inside it.

Importing division Feature from __future__

In older Python versions, dividing two integers would return an integer.

Importing the division feature (from future versions) changes the behavior of division operator "/" such that it always returns a float results (even if both operands are integers).

Example

In this example, we first print the result of integer division (as in Python 2), and then get a float result due to the __future__ import:

# Division in Python 2 style (without __future__)
print(5 / 2)  # May give 2 in Python 2

# Modern division behavior
from __future__ import division
print(5 / 2)

We get the output as shown below:

2
2.5

Importing print__function from __future__

In older Python versions (Python 2 and earlier), the print statement does not require the parentheses.

Importing print_function from the __future__ module makes the print statement behave like a regular function, requiring parentheses around the message.

Example

Following is the example of importing the print_function from the __future__ module:

from __future__ import print_function

# Now print must use parentheses
print("Hello from the future!")

Following is the output obtained:

Hello from the future!

Importing unicode_literals from __future__

In Python 2, string literals like 'hello' are stored as byte strings by default. But in Python 3, all string literals are Unicode by default.

By importing unicode_literals from __future__ module, though your current version is Python 2, the string literals you define are stored as Unicode values.

Example

In the following example, even though we used single quotes, the string becomes a Unicode string automatically due to the __future__() import:

from __future__ import unicode_literals

# All string literals are now Unicode by default
text = 'hello'
print(type(text))

This will generate the following output -

<class 'str'>

Using absolute_import from __future__

In older versions of Python, importing a module could accidentally load a local file instead of the standard library module.

We can avoid this by importing the specific module we require by using absolute_import feature.

Example

In this example, we make sure that Python imports the correct math module from the standard library:

from __future__ import absolute_import

# Will import standard library module, not a local file with same name
import math
print ("Imported Successfully")

Following is the output of the above program:

Imported Successfully

Using generator_stop from __future__

In older versions of Python, if you manually raise a StopIteration exception inside a generator, it would silently stop the iteration (loop) without any warning.

The generator_stop feature changes this behavior by turning it into a RuntimeError, helping to catch bugs more easily.

Example

In this example, raising StopIteration inside the generator results in a RuntimeError, helping to avoid silent bugs:

from __future__ import generator_stop
def my_gen():
   yield 1
   raise StopIteration("This error will not be ignored")

for num in my_gen():
   print(num)

We get the following output:

1
Traceback (most recent call last):
  ...
RuntimeError: generator raised StopIteration

How does the __future__ Module Works

When you write from __future__ import feature, it tells the Python interpreter to enable the new behavior before running any other code.

That's why this import statement should always be placed at the very top of your script, right after any comments or module docstrings.

Conclusion

The __future__ module helps for a smooth transition between Python versions by allowing you to use new features before they become standard. It is a helpful way to future-proof your code and avoid unexpected issues when upgrading Python versions.

Updated on: 2025-07-13T00:03:01+05:30

363 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements