PyCharm Django project fails to run with debugging

2019-03-29 13:00发布

问题:

For quite a while I have this weird problem on my Windows 10 machine. When trying to launch a Django server with (and sometimes even without) debugging on PyCharm, I get this error:

...
pydev debugger: process 10532 is connecting
Connected to pydev debugger (build 163.8233.8)
Process finished with exit code -1073741819 (0xC0000005)

That's how it looks without debugging:

"C:\Program Files (x86)\PyCharm\bin\runnerw.exe" D:\project\env\Scripts\python.exe D:/project/manage.py runserver 80

Process finished with exit code -1073741819 (0xC0000005)

The strangest thing is that if I re-run, some of the times (1 success in about 4-6 reruns) it will launch successfully. I also couldn't reproduce this problem without PyCharm.

Specs: Windows 10 Pro, Pycharm (this happens on versions 5.x through 2016.3) x86 and x64 versions, Python 3.5x, Django 1.8-1.10.x.

Any ideas why this happens or how to get some more detailed info about the crash?

EDIT This is what the Event Viewer reports about the error:

Faulting application name: python.exe, version: 3.5.1150.1013, time stamp: 0x566391f0
Faulting module name: ucrtbase.dll, version: 10.0.14393.0, time stamp: 0x57898db2
Exception code: 0xc0000005
Fault offset: 0x000b6d95
Faulting process ID: 0x1b30
Faulting application start time: 0x01d251286ca8ada3
Faulting application path: D:\project\env\Scripts\python.exe
Faulting module path: C:\WINDOWS\System32\ucrtbase.dll
Report ID: b23376d6-cf66-4d83-8781-cd22311e0f9e
Faulting package full name: 
Faulting package-relative application ID: 

EDIT 2 I've narrowed down the problem. The error appears to originate from restart_with_reloader in autoreload.py after this statement:

def restart_with_reloader():
    while True:
        args = [sys.executable] + ['-W%s' % o for o in sys.warnoptions] + sys.argv
        if sys.platform == "win32":
            args = ['"%s"' % arg for arg in args]
        new_environ = os.environ.copy()
        new_environ["RUN_MAIN"] = 'true'

        # this line crashes the whole script
        # I guess the problem arises in native code
        exit_code = os.spawnve(os.P_WAIT, sys.executable, args, new_environ)
        # debugger disconnects here

        if exit_code != 3:
            return exit_code

Maybe someone from the Python community has seen something similar?

回答1:

tl;dr solution

To Environment variables in Run/Debug Configurations dialog add this pair:

=C: c:\''


I have submitted a ticket to JetBrains YouTrack and this is actually a problem with how Windows handles command line environment variables. You can find a workaround for the problem here: https://youtrack.jetbrains.com/issue/PY-17069

Excerpt from JetBrains dev comment:

Guys, I just found a bug in Windows and Microsoft CRT:) Try to add following env variable to environment variables in run config =C: = c:`` Yes, =C: is NAME and c:`` is value.



回答2:

I had the same issue with PyCharm and Django. It's caused by running the auto reload feature of Django under PyCharm. I run PyCharm as admin as Jithin Pavithran suggested in the comment and problem was solved. You can also turn on "No reload" in PyCharm Django server configuration.