How to Fix Fatal Python Error: PyThreadState_Get: No Current Thread
-
What Causes
PyThreadState_Get: no current thread
Error -
Use
sudo
to SolvePyThreadState_Get: no current thread
Error -
Use
otool
to SolvePyThreadState_Get: no current thread
Error
On our local machine, we could have different Python version installations which can only do what’s limited or new to that version at any given time.
We can get issues if we write code within a version context with all its dependencies and that code runs in another Python version context.
One of these issues is the PyThreadState_Get: no current thread
, and in this article, we will discuss what causes it and how to resolve it on your local Mac/Linux PC.
What Causes PyThreadState_Get: no current thread
Error
Multiple Python installations have different modules and dependencies attached to them, but when we run Python code, the wrong Python binding might end up linking against the system provided by our PC. This scenario often happens when using macOS.
Therefore, we must ensure that the right Python (with our modules) runs our code and links the right modules to our system Python.
Use sudo
to Solve PyThreadState_Get: no current thread
Error
If you have multiple Python installations, different libraries may use Python, and to solve the PyThreadState_Get: no current thread
error message, we can change the active
Python to the Python installation that as the default libraries that you were calling previously within your code.
To change the active
Python installation, we can use the below sudo
command.
sudo port select --list python
The output of the command:
Available versions for python:
none
python26-apple
python27
python27-apple (active)
python34
From the above output, the active Python installation is python27-apple
, and to switch the Python installation that holds the dependencies and modules, you need python34
.
To change to python34
, we can use the sudo
command below.
sudo port select python python34
After we execute the above command, we can check if it worked by using the --list
command.
sudo port select --list python
The command output should now show that python34
is active.
Available versions for python:
none
python26-apple
python27
python27-apple
python34 (active)
Use otool
to Solve PyThreadState_Get: no current thread
Error
Often, the main cause of the PyThreadState_Get: no current thread
error message when running your code is often the use of a library (import
statement) with another Python installation that doesn’t recognize it. We can solve the problem using the otool
and install_name_tool
utilities.
To solve it, we will need to use the Python library (for example, Leap Motion) with the alternate Python installation and must update the library’s loader path to reference the Python installation we want.
First, use the otool
command to check the current loader paths.
otool -L LeapPython.so
The output of the command:
LeapPython.so:
@loader_path/LeapPython.so (compatibility version 0.0.0, current version 0.0.0)
/Library/Frameworks/Python.framework/Versions/3.4/Python (compatibility version 3.4.0, current version 3.4.0)
@loader_path/libLeap.dylib (compatibility version 0.7.0, current version 2.0.1)
/usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 169.3.0)
So we need to change and update the current Python location/reference, /Library/Frameworks/Python.framework/Versions/3.4/Python
, to our desired Python installation. To do that, we need to use the install_name_tool
command.
Once we execute the command, we have referenced the new Python installation location to the library we want to use.
install_name_tool -change /Library/Frameworks/Python.framework/Versions/3.4/Python \
/usr/local/Cellar/python/3.8.8/Frameworks/Python.framework/Versions/3.8/lib/libpython3.8.dylib \
LeapPython.so
Now, we can run our Python code without facing the PyThreadState_Get: no current thread
error message.
Olorunfemi is a lover of technology and computers. In addition, I write technology and coding content for developers and hobbyists. When not working, I learn to design, among other things.
LinkedInRelated Article - Python Error
- Can Only Concatenate List (Not Int) to List in Python
- How to Fix Value Error Need More Than One Value to Unpack in Python
- How to Fix ValueError Arrays Must All Be the Same Length in Python
- Invalid Syntax in Python
- How to Fix the TypeError: Object of Type 'Int64' Is Not JSON Serializable
- How to Fix the TypeError: 'float' Object Cannot Be Interpreted as an Integer in Python