How to resolve cannot import name ‘int_classes’ from ‘torch._six'

When using PyTorch, ImportError: cannot import name ‘int_classes’ from ‘torch._six’, this problem may appear. This error happens if the code being executed and the PyTorch version being utilized are incompatible.

Methods to fix ImportError: cannot import name ‘int_classes’ from ‘torch._six’

The easiest way to resolve this error is by simply using int_classes = int instead of importing it from ._six

Other methods include:

  • Update Torch: Make sure that your Torch version is up-to-date. You can check this by running the following command in the terminal.

pip install torch --upgrade
Updating version of torch
  • Compatibility check: Make sure your code is compatible with the installation of Torch. It's critical to confirm compatibility because newer versions may have altered or eliminated some features or functions.

  • Verify dependencies: Check your project or script's dependencies to ensure no outdated or incompatible packages exists. The problem might also be fixed by upgrading or reinstalling the packages that depend on Torch.

  • Restart the Python environment: In some cases, an outdated Python environment may cause the issue. Clearing any cached or incompatible modules can be accomplished by restarting your Python interpreter or the complete development environment.

  • Reinstall Torch: In some cases, reinstalling the Torch resolves the issue. So, you can reinstall it using these commands.

pip uninstall torch
pip install torch
Reinstalling Torch

Check the documentation and forums of any frameworks built on top of Torch, such as PyTorch or FastAI, to see if there are any known problems or specific installation guidelines.

Remember to update any pertinent dependencies and guarantee package compatibility throughout your project.

Free Resources

Copyright ©2025 Educative, Inc. All rights reserved