skip to Main Content

I have tried to run a program using pytorch. It stopped with the error ‘No module named "unittest.mock"’
Then I wrote a minimal python program:

import unittest.mock
if __name__ == '__main__':
    print("Hello world")

The same error. Documentation in https://docs.python.org/3.11/library/unittest.mock.html#module-unittest.mock. This module should be builtin since 3.3

Environment: python 3.11.2, Debian-Bookworm

Running in Debian-Bullseye with python 3.9: the same problem

2

Answers


  1. If you encounter the error "No module named ‘unittest.mock’" when running a program that uses PyTorch, it typically indicates that your Python environment may be configured incorrectly or that you are using an older Python version that does not support this module. unittest.mock is a module for mocking objects in unit tests, available in Python 3.3 and later versions. To resolve this issue, first confirm that your Python version is at least 3.3 and check if your environment variables (such as PYTHONPATH) are set correctly. If the problem persists, consider using a virtual environment (like venv or conda) to avoid conflicts between libraries and ensure that the PyTorch version you have installed is compatible with your Python version. Additionally, although unittest.mock was deprecated in Python 3.8, it remains usable, and you can import it via from unittest import mock. If possible, updating your code to utilize the latest features and best practices of Python is also recommended.

    Login or Signup to reply.
  2. If you see "No modue named ‘unittestmock’" when unning PyTorch, your Python might be old o set up rong. Make sre you’re on Python 3.3 or ewe, check your setngs, and use a virtual environent. Even though unittest.mock is old now, you can still use it. And try to keepyour code fresh with the newest Python stoff.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search