Bash: !': event not found

I am using Cursor with Claude Opus for working with Python code. It would often try something like

cd my_dir && python -c "from my_module import Something; more_python_code; print('Some message about being done!')"

Unfortunately that ! at the very end is a problem, resulting in the code not running and just getting

bash: !': event not found

What’s worse is that at this point the chat would just sit there waiting for more to happen in the shell session, while the shell session is actually idle and not doing anything, so the chat does not get an opportunity to correct the error…