Search code examples
pythonwindows-10spyderpywin32pythoncom

Spyder is throwing "assert sys.modules[modname] is not old_mod" when running pythoncom code


Edited: If I run the following code two times via spyder, it gives me an error the second time I run it, the first time it always works:

import pythoncom

if __name__ == '__main__':
    print("hallo")

This is the error:

File "C:\Users\Oliver\AppData\Local\Programs\Python\Python36-32\lib\site-packages\win32\lib\pywintypes.py", line 129, in __import_pywin32_system_module__
    assert sys.modules[modname] is not old_mod

When I restart the kernel, it works again. Also if I run it directly using python, it always works.


Solution

  • (Spyder maintainer here) This problem is caused by Spyder User Module Reloader (UMR), which reloads modules in our consoles so that changes can be picked up interactively on them.

    To fix it, you need to add pythoncom to the list of UMR blacklisted modules by going to:

    Tools > Preferences > Python interpreter > Set UMR excluded (not reloaded) modules.

    Note: If you also want to prevent our UMR to show the list of reloaded modules every time you run a file, you can turn off its warnings by deactivating this option:

    Tools > Preferences > Python interpreter > Show reloaded modules list