Tag Archives: Fatal Python error: PyThreadState_Get: no current thread

Paddlepaddle: fatal Python error: PyThreadState appears in the code running Boston house price forecast_ Get: no current thread

Problem description.
After successfully installing PaddlePaddle, running the code for Boston House Price Prediction reports the error Fatal Python error: PyThreadState_Get: no current</code

Error output.

Fatal Python error: PyThreadState_Get: no current thread

recurrence mode:
there are two versions of Python 2.7 and python 2.7 installed by brew in MAC. Install paddlepaddle in python of anaconda, and the installation is successful. Use the successfully installed paddlepaddle to execute the room prediction model report fatal Python error: PyThreadState_ Get: no current thread

solution:
this problem is caused by the conflict between brew’s Python and Anaconda’s python. The solution is as follows:

Execute otool - L/anaconda2/lib/python2.7/site-packages/py_ paddle/_ swig_ paddle.so

The output is as follows:

/anaconda2/lib/python2.7/site-packages/py_paddle/_swig_paddle.so 
/System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation (compatibility version 150.0.0, current version 1445.12.0) 
/System/Library/Frameworks/Security.framework/Versions/A/Security (compatibility version 1.0.0, current version 58286.20.16) 
/usr/local/opt/python/Frameworks/Python.framework/Versions/2.7/Python (compatibility version 2.7.0, current version 2.7.0) 
/usr/lib/libc++.1.dylib (compatibility version 1.0.0, current version 400.9.0) 
/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1252.0.0)

It can be found that there is no/usr/local/opt/Python/frameworks/python. Framework/versions/2.7/python path

Execute install_ name_ tool -change /usr/local/opt/python/Frameworks/Python.framework/Versions/2.7/Python /anaconda/lib/libpython2.7.dylib /anaconda/lib/python2.7/site-packages/py_ paddle/_ swig_ paddle.so

At this point, we can run the Boston house price forecast code through paddlepaddle, and the above problem will not appear again

problem analysis:
PyThreadState_ Get () method is a method in the python kernel. This method is mainly used for the operation of Python threads. Threads actually involve the call of system resources. When there are many different Python in the system and there is no environment isolation, the problem of Python version conflict may occur. The manifestation of the conflict problem may be fatal Python error: PyThreadState_ Get: no current thread , because it’s kernel level code, we usually don’t need to modify, and it’s difficult to modify, and the cost is too high, so the more recommended method is to modify the environment in the system, such as the method used in the solution, and modify the python development environment through the corresponding configuration, so as to avoid the occurrence of Python version conflict

problem development:
generally speaking, problems at the kernel level are serious, so problems at this level can be quickly fixed. If problems at this level occur in the stable version of Python you are using, they are usually environmental problems, such as version conflicts or system resource restrictions, To solve this problem, the best way is to control the version of Python. Usually, tools such as pyenv and virtualenv can be used. Pyenv only supports Linux and MAC. These tools can be used to create independent virtual development environments for different versions of Python. These development environments will not affect the local environment and are well isolated. Of course, for specific problems, For example, fatal Python error: PyThreadState_ Get: no current thread can also use specific solutions

problem research:
PyThreadState_ Get is a method in the python kernel. Some of its kernel codes are as follows:

void PyErr_Restore(PyObject *type, PyObject *value, PyObject *traceback)
{
     
     
     
    PyThreadState *tstate = PyThreadState_GET();
    PyObject *oldtype, *oldvalue, *oldtraceback;

    if (traceback != NULL && !PyTraceBack_Check(traceback)) {
     
     
     
        /* XXX Should never happen -- fatal error instead?*/
        /* Well, it could be None. */
        Py_DECREF(traceback);
        traceback = NULL;
    }

    // Save previous exception messages
    oldtype = tstate->curexc_type;
    oldvalue = tstate->curexc_value;
    oldtraceback = tstate->curexc_traceback;
    // Set the current exception message
    tstate->curexc_type = type;
    tstate->curexc_value = value;
    tstate->curexc_traceback = traceback;
    // Discard previous exception messages
    Py_XDECREF(oldtype);
    Py_XDECREF(oldvalue);
    Py_XDECREF(oldtraceback);
}

Python through PyThreadState_ Get () can get the current thread and store the exception information in the thread state object

The Python kernel level code usually does not report any errors, but if it encounters errors at this level, the first thing to consider is still the development environment_ Get: no current thread , it usually appears in the MAC system. The common reason is that there are multiple Python environments in the Mac. An elegant way is to use pyenv on the Mac, so that you can isolate the original code of the system through pyenv. The python installed by brew is isolated from other Python installed later