1

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.

Carlos Cordoba
  • 33,273
  • 10
  • 95
  • 124
Dan
  • 49
  • 1
  • 8
  • I'm not familiar with the `dash` module. Does it actually have anything to do with the Debian Almquist shell? – Andrey Tyukin May 20 '18 at 20:42
  • I assume `dash` is [this "framework for building reactive web-apps"](https://pypi.org/project/dash/)? If so, better to edit the question to say that (with the link). And definitely remove the tag, which is for a completely different thing. – abarnert May 20 '18 at 20:49
  • Anyway, the problem seems to be related to the `pythoncom` module from the `pywin32` package. Not sure why a web framework would import that, but… Anyway, can you try running something that does an `import pythoncom` twice, and see if that has the same problem? If so, it would probably narrow things down to a much smaller traceback, and also involve a much more popular library (there are lots of `pywin32` experts around to help you, but probably only a handful of people using `dash`). – abarnert May 20 '18 at 20:52
  • Here you go, [a new tag extra for this framework](https://stackoverflow.com/questions/tagged/dash-framework). – Andrey Tyukin May 20 '18 at 21:05
  • @abarnert If I do "import pythoncom print("a")" I got the same error after runnign the code a second time. To clarify: dash is related to this: https://plot.ly/products/dash/ – Dan May 20 '18 at 22:08
  • 1
    First, edit useful information into the question, don't just put it in comments. Also, as I said, you may want to rewrite your question to be about using `pythoncom`. Notice that `dash-framework` has 0 followers, 1 question, and 0 answers, while `pywin32` has a ton of each. There are a lot more experts on a well-established, widely-used library than on a new one nobody's heard of. Also, since the root problem seems to be with `pywin32`, their experts will be more useful. And, finally, it probably gives you a shorter traceback that will just turn fewer people off from answering because tl;dr. – abarnert May 20 '18 at 22:18
  • I ran your code, twice..exactly..and i was unable to reproduce the error – rawwar May 21 '18 at 08:19
  • @Kalyan Did you run it on spyder or directly with python? For python everything is fine for me, but if I use spyder I got the error the second time I run it. – Dan May 21 '18 at 18:06
  • 1
    My wild guess is that this has something to do with Spyder doing some COM stuff with the `pythoncom` library (which generally means dynamically creating a bunch of classes, etc. to match the COM objects you're trying to talk to) that means you can't reload the `pythoncom` library from inside Spyder. (If this is a real problem that can happen, that's something that `pywin32`/`pythoncom` experts are likely to know; I haven't used it for anything serious in a decade or more…) – abarnert May 21 '18 at 18:28
  • I ran it in spyder – rawwar May 21 '18 at 18:59
  • @Kalyan spyder 3 python 3.6.4? – Dan May 21 '18 at 19:48
  • As said in comments, remove that long (and useless) traceback, Add all the info from comments (Including *Python*, *pywin32*, *OS* versions), in the question, and also mention that it happens in *spyder* (might want to add it as a tag). – CristiFati May 22 '18 at 07:45
  • (*Spyder maintainer here*) @Olli, I think this error can be related to our User Module Reloader, which reloads modules in our consoles so that changes can be picked up interactively on them. To see if that's true, please add `pythoncom` to `Preferences > Python interpreter > Set UMR excluded (not reloaded) modules`, restart Spyder and try again. – Carlos Cordoba May 22 '18 at 08:18
  • @CarlosCordoba Thank you. When I run it now a second time I get no error, only a long list of "Reloaded modules:". – Dan May 22 '18 at 10:52
  • @Olli, it's great to know that you're not getting that error anymore. If you don't want to see that list of "Reloaded modules", you can simply deactivate that option (i.e. `Show reloaded modules list`), which is present in the same menu entry I mentioned before. – Carlos Cordoba May 22 '18 at 17:25
  • @CarlosCordoba Thank you. Everything works now. As you only answered in comments and not to my original question I think I cannot mark your comment as answer to my original problem. If you want, you can reply to my original post and I can mark your answer as solution to my problem. – Dan May 23 '18 at 18:22
  • @Olli, I wasn't sure if what I told you was going to solve your problem. But now that you confirmed that, I can write a proper answer. – Carlos Cordoba May 23 '18 at 19:03

1 Answers1

3

(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

Carlos Cordoba
  • 33,273
  • 10
  • 95
  • 124