Django 4 Pycharm debugger error: can't find '__main__' module in ''

My Django project is working properly when I am pressing pycharm's green play button. But when debug button is clicked, it says can't find '__main__' module in ''.

I did little bit RCA and found __spec__ is being set on __main__ when running with the debug option.

Here is the relevant code degment from /lib/python3.8/site-packages/django/utils/autoreload.py

# __spec__ is set when the server was started with the `-m` option,
# see https://docs.python.org/3/reference/import.html#main-spec
# __spec__ may not exist, e.g. when running in a Conda env.
if getattr(__main__, '__spec__', None) is not None:
    spec = __main__.__spec__
    if (spec.name == '__main__' or spec.name.endswith('.__main__')) and spec.parent:
        name = spec.parent
    else:
        name = spec.name
    args += ['-m', name]
    args += sys.argv[1:]

Please help me finding a workaround in order to use PyCharm's debug functionality.

Django version: 4.0.1

Python version: 3.8.6

PyCharm 2021.2 (Community Edition)


TL:DR - try to update PyCharm

Got this same problem, with almost identically versions of Django, Python and PyCharm. Tried many ways to solve it, but the only way which was working was... updating PyCharm;) it is a problem with venv I think, because when I was changing the interpreter to any other (e.g. from other django projects) it worked with some of them, but not with all (although the same packages). When I was digging very deep with debugger, it looks like the problem was adding '-m' and '__main__' to the args container after this '__spec__' attribute. With the use of some other venv's it wasn't adding anything, except of 'runserver' and worked fine. I wanted to simply start new project or recreate the venv, but first I have had updated PyCharm. One restart of PyCharm and (I don't know how, but...) it started to working properly. And now it is not adding any other parameters to args...