'sys.exit() with custom message returns error when run in IPython console in PyCharm

I have the following (very simple) Python script that I'm trying to run in PyCharm:

import sys
sys.exit('My custom message')

In the "Run/Debug Configurations" window of PyCharm, if "Run with Python Console" is unchecked, the execution of the script works fine and provides:

My custom message

Process finished with exit code 1

However, if "Run with Python Console" is checked, the execution is performed with IPython and provides an error:

Traceback (most recent call last):
  File "/snap/pycharm-community/267/plugins/python-ce/helpers/pydev/pydevconsole.py", line 331, in do_exit
    import java.lang.System
  File "/snap/pycharm-community/267/plugins/python-ce/helpers/pydev/_pydev_bundle/pydev_import_hook.py", line 21, in do_import
    module = self._system_import(name, *args, **kwargs)
ModuleNotFoundError: No module named 'java'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "/home/toto/python_envs/mambaforge/envs/env002/lib/python3.10/site-packages/IPython/core/interactiveshell.py", line 3251, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-2-e62ee88d3202>", line 1, in <module>
    runfile('/home/toto/code/test.py', wdir='/home/toto/code')
  File "/snap/pycharm-community/267/plugins/python-ce/helpers/pydev/_pydev_bundle/pydev_umd.py", line 198, in runfile
    pydev_imports.execfile(filename, global_vars, local_vars)  # execute the script
  File "/snap/pycharm-community/267/plugins/python-ce/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "/home/toto/code/test.py", line 3, in <module>
    sys.exit('error')
  File "/snap/pycharm-community/267/plugins/python-ce/helpers/pydev/pydevconsole.py", line 336, in do_exit
    os._exit(args[0])
TypeError: 'str' object cannot be interpreted as an integer

I'd like to test my codes in IPython and don't know how to fix this error. I think my code is correct, so this should be a bug in PyCharm.

Many thanks for your help!



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source