SyntaxError: future feature annotations is not defined

avatar
Borislav Hadzhiev

Last updated: Apr 13, 2024
3 min

banner

# Table of Contents

  1. SyntaxError: future feature annotations is not defined
  2. Creating a virtual environment scoped to a specific Python version
  3. Installing a more recent Python version via Anaconda
  4. Installing a more recent version of Python to solve the error

# SyntaxError: future feature annotations is not defined

The Python error "SyntaxError: future feature annotations is not defined" occurs when the future behavior for annotations is used in a Python version less than 3.7.

The functionality is available starting with Python version 3.7.

Here is an example of how the error occurs.

main.py
# Python version 3.6 from __future__ import annotations print('bobbyhadz.com')

Running the code sample produces the following output.

shell
from future import annotations ^ SyntaxError: future feature annotations is not defined

The issue in the code sample is that I'm running the code sample in Python version 3.6.

As shown in this PEP 563 proposal the future behavior is available starting with Python 3.7.

You can check your Python version with the python --version command.

shell
python --version # Or python3 python3 --version # Or with py alias on Windows py --version

You need to be running at least version 3.7 of the Python interpreter to be able to include the import statement.

check python version

# Creating a virtual environment scoped to a specific Python version

If you have a Python installation with a version greater than 3.7, try to create a virtual environment scoped to the specific version.

shell
# ๐Ÿ‘‡๏ธ Create a virtual environment python3 -m venv venv

Make sure to create the virtual environment with a Python interpreter that runs a version greater than 3.7.

If the python3 -m venv venv command doesn't work, try one of the following commands:

  • python -m venv venv
  • py -m venv venv
  • python3.10 -m venv venv
  • python3.11 -m venv venv

The version of Python in your virtual environment will be the version of Python that was used to create it.

Once you've created the virtual environment, activate it by running one of the following commands.

shell
# ๐Ÿ‘‡๏ธ Activate on Windows (cmd.exe) venv\Scripts\activate.bat # ๐Ÿ‘‡๏ธ Activate on Windows (PowerShell) venv\Scripts\Activate.ps1 # ๐Ÿ‘‡๏ธ Activate on Unix or MacOS source venv/bin/activate

Now, verify that your virtual environment runs a Python version greater than 3.7.

shell
python --version

If the output shows a version greater than 3.7, you should be able to rerun your Python script without getting errors.

# Installing a more recent Python version via Anaconda

If you have Anaconda installed, you can use the conda command to install a more recent Python version.

shell
conda install python=3.10.6

install more recent python version using anaconda

You can also create an Anaconda virtual environment that runs a more recent Python version.

shell
conda create --name my_env -c anaconda python=3.10.6

# Installing a more recent version of Python to solve the error

Alternatively, you can install a more recent version of Python to solve the error.

  1. Visit the official python.org website.
  2. Click on the download button below the Download the latest source release header.

download more recent version of python

Once you download a Python version greater than 3.7, you should be able to rerun your Python script without getting any errors.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev