I found another SO addressing a similar problem but no one gave an answer.
My problem is the following (and a bit simpler to reproduce), create a file called test_file.py
with:
import psutil
import xlwings
def test() -> None:
my_function()
def my_function() -> None:
# Create a new workbook
wb = xlwings.Book()
# Save it
wb.save('test.xlsx')
# Close it
wb.close()
# Kill zombie processes
for proc in psutil.process_iter():
if proc.name() == "EXCEL.EXE":
proc.kill()
if __name__ == "__main__":
my_function()
When doing python test_file.py
, everything goes smoothly.
When doing pytest
to do my unit tests, I have this error:
test_file.py Windows fatal exception: code 0x800706ba
The traceback is the following:
File "C:\Users\jason\Desktop\so\test_file.py", line 6 in test
File "C:\Users\jason\AppData\Local\Programs\Python\Python311\Lib\site-packages\_pytest\python.py", line 195 in pytest_pyfunc_call
File "C:\Users\jason\AppData\Local\Programs\Python\Python311\Lib\site-packages\pluggy\_callers.py", line 39 in _multicall
File "C:\Users\jason\AppData\Local\Programs\Python\Python311\Lib\site-packages\pluggy\_manager.py", line 80 in _hookexec
File "C:\Users\jason\AppData\Local\Programs\Python\Python311\Lib\site-packages\pluggy\_hooks.py", line 265 in __call__
File "C:\Users\jason\AppData\Local\Programs\Python\Python311\Lib\site-packages\_pytest\python.py", line 1789 in runtest
File "C:\Users\jason\AppData\Local\Programs\Python\Python311\Lib\site-packages\_pytest\runner.py", line 167 in pytest_runtest_call
File "C:\Users\jason\AppData\Local\Programs\Python\Python311\Lib\site-packages\pluggy\_callers.py", line 39 in _multicall
File "C:\Users\jason\AppData\Local\Programs\Python\Python311\Lib\site-packages\pluggy\_manager.py", line 80 in _hookexec
File "C:\Users\jason\AppData\Local\Programs\Python\Python311\Lib\site-packages\pluggy\_hooks.py", line 265 in __call__
File "C:\Users\jason\AppData\Local\Programs\Python\Python311\Lib\site-packages\_pytest\runner.py", line 260 in <lambda>
File "C:\Users\jason\AppData\Local\Programs\Python\Python311\Lib\site-packages\_pytest\runner.py", line 339 in from_call
File "C:\Users\jason\AppData\Local\Programs\Python\Python311\Lib\site-packages\_pytest\runner.py", line 259 in call_runtest_hook
File "C:\Users\jason\AppData\Local\Programs\Python\Python311\Lib\site-packages\_pytest\runner.py", line 220 in call_and_report
File "C:\Users\jason\AppData\Local\Programs\Python\Python311\Lib\site-packages\_pytest\runner.py", line 131 in runtestprotocol
File "C:\Users\jason\AppData\Local\Programs\Python\Python311\Lib\site-packages\_pytest\runner.py", line 112 in pytest_runtest_protocol
File "C:\Users\jason\AppData\Local\Programs\Python\Python311\Lib\site-packages\pluggy\_callers.py", line 39 in _multicall
File "C:\Users\jason\AppData\Local\Programs\Python\Python311\Lib\site-packages\pluggy\_manager.py", line 80 in _hookexec
File "C:\Users\jason\AppData\Local\Programs\Python\Python311\Lib\site-packages\pluggy\_hooks.py", line 265 in __call__
File "C:\Users\jason\AppData\Local\Programs\Python\Python311\Lib\site-packages\_pytest\main.py", line 349 in pytest_runtestloop
File "C:\Users\jason\AppData\Local\Programs\Python\Python311\Lib\site-packages\pluggy\_callers.py", line 39 in _multicall
File "C:\Users\jason\AppData\Local\Programs\Python\Python311\Lib\site-packages\pluggy\_manager.py", line 80 in _hookexec
File "C:\Users\jason\AppData\Local\Programs\Python\Python311\Lib\site-packages\pluggy\_hooks.py", line 265 in __call__
File "C:\Users\jason\AppData\Local\Programs\Python\Python311\Lib\site-packages\_pytest\main.py", line 324 in _main
File "C:\Users\jason\AppData\Local\Programs\Python\Python311\Lib\site-packages\_pytest\main.py", line 270 in wrap_session
File "C:\Users\jason\AppData\Local\Programs\Python\Python311\Lib\site-packages\_pytest\main.py", line 317 in pytest_cmdline_main
File "C:\Users\jason\AppData\Local\Programs\Python\Python311\Lib\site-packages\pluggy\_callers.py", line 39 in _multicall
File "C:\Users\jason\AppData\Local\Programs\Python\Python311\Lib\site-packages\pluggy\_manager.py", line 80 in _hookexec
File "C:\Users\jason\AppData\Local\Programs\Python\Python311\Lib\site-packages\pluggy\_hooks.py", line 265 in __call__
File "C:\Users\jason\AppData\Local\Programs\Python\Python311\Lib\site-packages\_pytest\config\__init__.py", line 167 in main
File "C:\Users\jason\AppData\Local\Programs\Python\Python311\Lib\site-packages\_pytest\config\__init__.py", line 190 in console_main
File "C:\Users\jason\AppData\Local\Programs\Python\Python311\Scripts\pytest.exe\__main__.py", line 7 in <module>
File "<frozen runpy>", line 88 in _run_code
File "<frozen runpy>", line 198 in _run_module_as_main
It must be a problem between pytest
, xlwings
, psutil
because if I'm not using the three simultaneously I cannot reproduce the bug.
I'm using Python 3.11.1
, xlwings==0.28.6
, pytest==7.2.2
and psutil==5.9.4
.
Using a context manager as suggested by @moken doesn't work either:
import xlwings
def test() -> None:
my_function()
def my_function() -> None:
with xlwings.App(visible=True):
# Create a new workbook
wb = xlwings.Book()
# Save it
wb.save('test.xlsx')
# Close it
wb.close()
if __name__ == "__main__":
my_function()
raises the exact same error (when using pytest
, when not, everything works fine obviously).