Last modified: Aug 11, 2026

Fix ModuleNotFoundError: No module named 'torch._six'

Encountering ModuleNotFoundError: No module named 'torch._six' can be frustrating. This error typically appears when using older PyTorch code with newer versions of the library. The torch._six module was a private utility, removed after PyTorch 1.13. Let's dive into why this happens and how to fix it.

Understanding the Error

This error is a direct result of PyTorch's internal changes. The torch._six module was used for Python 2 and 3 compatibility. It contained functions like string_classes and int_classes. As PyTorch dropped Python 2 support, this module became obsolete and was deleted.

When your code or a third-party library still references it, Python cannot find it. This triggers the ModuleNotFoundError. The fix is to update your code or the dependency that uses it.

Quick Fix: Upgrade or Downgrade PyTorch

The simplest immediate solution is to adjust your PyTorch version. If you are using a very old version, upgrading is best. If you have a new version but old code, you might need to modify the code itself.

First, check your current PyTorch version using this command:


python -c "import torch; print(torch.__version__)"

If your version is below 1.13, the torch._six module exists. If it's 2.0 or higher, it's gone. For a quick fix, you can downgrade to PyTorch 1.12. However, this is not recommended for security and performance reasons.

Instead, upgrade to the latest stable version. This often resolves the issue if the error comes from an outdated package that has since been updated.


pip install --upgrade torch torchvision torchaudio

Manual Code Fix: Replace torch._six

If upgrading doesn't work, you need to edit your code. The most common usage is from torch._six import string_classes. You can replace this with standard Python imports.

Here is a typical example of what you might find in old code:


# Old code that causes the error
from torch._six import string_classes, int_classes

# Your custom dataset class
class MyDataset(torch.utils.data.Dataset):
    def __init__(self):
        self.data = [1, 2, 3]
        self.labels = ['a', 'b', 'c']

To fix it, replace the import with Python's built-in types. Use str for string_classes and int for int_classes. Here's the corrected version:


# Fixed code using standard Python types
import torch

# Replace with built-in types
string_classes = str
int_classes = int

# Your custom dataset class
class MyDataset(torch.utils.data.Dataset):
    def __init__(self):
        self.data = [1, 2, 3]
        self.labels = ['a', 'b', 'c']
        
    def __len__(self):
        return len(self.data)

This simple replacement works in most cases. The torch._six module was just a wrapper for these standard types.

Finding the Source of the Error

Sometimes the error is not in your direct code. It could be in a third-party library. To locate it, read the full traceback. The error message will show the file path causing the problem.

For instance, if you see a path like site-packages/some_library/module.py, that library is the culprit. You have a few options then:

  • Update the library to a newer version that supports current PyTorch.
  • Contact the library maintainers or check their GitHub for a fix.
  • Monkey-patch the library as a temporary workaround.

Monkey-patching is a quick hack. You can manually add the missing module to torch._six before the library imports it. Add this at the very beginning of your script:


# Temporary workaround - add at the top of your script
import torch
import sys
import types

# Create a fake torch._six module
fake_six = types.ModuleType('torch._six')
fake_six.string_classes = str
fake_six.int_classes = int
sys.modules['torch._six'] = fake_six

# Now import your library that needs torch._six
import some_old_library

This creates a dummy module that mimics the old one. It's not a permanent solution, but it gets you running immediately.

Preventing Future Issues

To avoid this error in the future, always keep your dependencies updated. Use a virtual environment to manage packages for different projects. This prevents version conflicts.

Also, check the compatibility of your libraries. Before installing a new package, verify it supports your current PyTorch version. Reading the package's documentation or release notes helps.

When writing new code, avoid using private modules like torch._six. They are not part of the public API and can change anytime. Stick to public, documented APIs.

Here's a checklist to follow:

  • Use pip list to see all installed packages.
  • Use pip check to find broken dependencies.
  • Always run your code in a fresh virtual environment.

Advanced: Using Compatibility Layers

Some projects use a compatibility layer to handle these changes. You can create your own helper module. This centralizes the fix and makes future updates easier.

Create a file called compat.py in your project:


# compat.py - Compatibility layer for torch._six
import torch

# Define the missing classes
string_classes = str
int_classes = int

# Add more if needed
# Example: container_abcs
import collections.abc as container_abcs

Then, in your main code, import from this file instead of torch._six:


# Instead of: from torch._six import string_classes
from compat import string_classes, int_classes

# Your code here
print(string_classes)  # Output: 

This approach is clean and maintainable. You only need to update the compat.py file if PyTorch changes again.

Conclusion

The ModuleNotFoundError: No module named 'torch._six' is a common issue when migrating to newer PyTorch versions. The root cause is the removal of a private utility module. You have several ways to fix it: upgrade PyTorch, edit your code to use standard Python types, or monkey-patch the missing module.

For most users, replacing torch._six.string_classes with str and torch._six.int_classes with int works perfectly. Always check the full traceback to find the exact source of the error. Keep your dependencies updated and avoid using private APIs in your own code.

By following these steps, you can resolve this error quickly and get back to building your deep learning models without interruption. Remember, a clean and updated environment prevents most of these issues from occurring in the first place.