Repository: spyder-ide/spyder

Issue:
Kernel error when using a Python 2 interpreter
## Description

### What steps will reproduce the problem?

<!--- You can use Markdown here --->

can't use the python2.7 in spyder after converting the defaut interpreter, and I also install the spyder_kernels by pip2 in python2 environment integrated in arcgis 10.7

### Traceback
```python-traceback
Exception in comms call get_namespace_view:
  File "C:\Python27\ArcGIS10.7\lib\site-packages\spyder_kernels\comms\commbase.py", line 346, in _handle_remote_call
    buffer['call_kwargs'])
  File "C:\Python27\ArcGIS10.7\lib\site-packages\spyder_kernels\comms\frontendcomm.py", line 267, in _remote_callback
    thread_id = threading.get_ident()
AttributeError: 'module' object has no attribute 'get_ident'
Exception in comms call get_var_properties:
  File "C:\Python27\ArcGIS10.7\lib\site-packages\spyder_kernels\comms\commbase.py", line 346, in _handle_remote_call
    buffer['call_kwargs'])
  File "C:\Python27\ArcGIS10.7\lib\site-packages\spyder_kernels\comms\frontendcomm.py", line 267, in _remote_callback
    thread_id = threading.get_ident()
AttributeError: 'module' object has no attribute 'get_ident'
Exception in comms call is_special_kernel_valid:
  File "C:\Python27\ArcGIS10.7\lib\site-packages\spyder_kernels\comms\commbase.py", line 346, in _handle_remote_call
    buffer['call_kwargs'])
  File "C:\Python27\ArcGIS10.7\lib\site-packages\spyder_kernels\comms\frontendcomm.py", line 267, in _remote_callback
    thread_id = threading.get_ident()
AttributeError: 'module' object has no attribute 'get_ident'
```

## Versions

* Spyder version: 5.4.3  (conda)
* Python version: 3.8.16 64-bit
* Qt version: 5.15.2
* PyQt5 version: 5.15.9
* Operating System: Windows 10

### Dependencies

```
# Mandatory:
atomicwrites >=1.2.0                             :  1.4.1 (OK)
chardet >=2.0.0                                  :  5.1.0 (OK)
cloudpickle >=0.5.0                              :  2.2.1 (OK)
cookiecutter >=1.6.0                             :  2.1.1 (OK)
diff_match_patch >=20181111                      :  20230430 (OK)
intervaltree >=3.0.2                             :  3.1.0 (OK)
IPython >=7.31.1,<9.0.0,!=8.8.0,!=8.9.0,!=8.10.0 :  8.12.2 (OK)
jedi >=0.17.2,<0.19.0                            :  0.18.2 (OK)
jellyfish >=0.7                                  :  0.11.2 (OK)
jsonschema >=3.2.0                               :  4.17.3 (OK)
keyring >=17.0.0                                 :  23.13.1 (OK)
nbconvert >=4.0                                  :  7.4.0 (OK)
numpydoc >=0.6.0                                 :  1.5.0 (OK)
paramiko >=2.4.0                                 :  3.1.0 (OK)
parso >=0.7.0,<0.9.0                             :  0.8.3 (OK)
pexpect >=4.4.0                                  :  4.8.0 (OK)
pickleshare >=0.4                                :  0.7.5 (OK)
psutil >=5.3                                     :  5.9.5 (OK)
pygments >=2.0                                   :  2.15.1 (OK)
pylint >=2.5.0,<3.0                              :  2.17.4 (OK)
pylint_venv >=2.1.1                              :  3.0.1 (OK)
pyls_spyder >=0.4.0                              :  0.4.0 (OK)
pylsp >=1.7.2,<1.8.0                             :  1.7.2 (OK)
pylsp_black >=1.2.0                              :  1.2.1 (OK)
qdarkstyle >=3.0.2,<3.2.0                        :  3.1 (OK)
qstylizer >=0.2.2                                :  0.2.2 (OK)
qtawesome >=1.2.1                                :  1.2.3 (OK)
qtconsole >=5.4.2,<5.5.0                         :  5.4.3 (OK)
qtpy >=2.1.0                                     :  2.3.1 (OK)
rtree >=0.9.7                                    :  1.0.1 (OK)
setuptools >=49.6.0                              :  67.8.0 (OK)
sphinx >=0.6.6                                   :  7.0.0 (OK)
spyder_kernels >=2.4.3,<2.5.0                    :  2.4.3 (OK)
textdistance >=4.2.0                             :  4.5.0 (OK)
three_merge >=0.1.1                              :  0.1.1 (OK)
watchdog >=0.10.3                                :  3.0.0 (OK)
zmq >=22.1.0                                     :  25.0.2 (OK)

# Optional:
cython >=0.21                                    :  None (NOK)
matplotlib >=3.0.0                               :  3.7.1 (OK)
numpy >=1.7                                      :  1.24.3 (OK)
pandas >=1.1.1                                   :  2.0.1 (OK)
scipy >=0.17.0                                   :  1.10.1 (OK)
sympy >=0.7.3                                    :  None (NOK)
```



Discussion:
Hi @Z-xk thank you for the feedback! Seems like this could be an `spyder-kernels` issue 🤔 What do yout think @ccordoba12 @impact27 ?
The thing is python2.7 is not supported anymore, so you will have to install an older version of Spyder and Spyder-kernels if you want to work with a depreciated version of python.
Since we still support Python 2 in Spyder-kernels, we need to address this. According to the [Python 2 docs](https://python.readthedocs.io/en/v2.7.2/library/thread.html), this should be a matter of replacing `threading.get_ident` by `thread.get_ident`.

@Z-xk, that fix will be available in our next version (5.5.0), to be released next month.

Resolving patch:
diff --git a/external-deps/spyder-kernels/spyder_kernels/comms/frontendcomm.py b/external-deps/spyder-kernels/spyder_kernels/comms/frontendcomm.py
--- a/external-deps/spyder-kernels/spyder_kernels/comms/frontendcomm.py
+++ b/external-deps/spyder-kernels/spyder_kernels/comms/frontendcomm.py
@@ -24,6 +24,10 @@
 from spyder_kernels.py3compat import TimeoutError, PY2
 
 
+if PY2:
+    import thread
+
+
 def get_free_port():
     """Find a free port on the local machine."""
     sock = socket.socket()
@@ -264,7 +268,7 @@ def _remote_callback(self, call_name, call_args, call_kwargs):
             current_stderr = sys.stderr
             saved_stdout_write = current_stdout.write
             saved_stderr_write = current_stderr.write
-            thread_id = threading.get_ident()
+            thread_id = thread.get_ident() if PY2 else threading.get_ident()
             current_stdout.write = WriteWrapper(
                 saved_stdout_write, call_name, thread_id)
             current_stderr.write = WriteWrapper(
@@ -300,7 +304,8 @@ def is_benign_message(self, message):
 
     def __call__(self, string):
         """Print warning once."""
-        if self._thread_id != threading.get_ident():
+        thread_id = thread.get_ident() if PY2 else threading.get_ident()
+        if self._thread_id != thread_id:
             return self._write(string)
 
         if not self.is_benign_message(string):
diff --git a/external-deps/spyder-kernels/spyder_kernels/console/start.py b/external-deps/spyder-kernels/spyder_kernels/console/start.py
--- a/external-deps/spyder-kernels/spyder_kernels/console/start.py
+++ b/external-deps/spyder-kernels/spyder_kernels/console/start.py
@@ -239,7 +239,8 @@ def kernel_config():
 
     # Disable the new mechanism to capture and forward low-level output
     # in IPykernel 6. For that we have Wurlitzer.
-    spy_cfg.IPKernelApp.capture_fd_output = False
+    if not PY2:
+        spy_cfg.IPKernelApp.capture_fd_output = False
 
     # Merge IPython and Spyder configs. Spyder prefs will have prevalence
     # over IPython ones


Test patch: