Repository: spyder-ide/spyder

Issue:
`This version of python seems to be incorrectly compiled` message with Python 3.11 from Anaconda
* Console banner:

```
Python 3.11.2 | packaged by Anaconda, Inc. | (main, Mar 27 2023, 23:35:04) [MSC v.1916 64 bit (AMD64)]Type "copyright", "credits" or "license" for more information.IPython 8.10.0 -- An enhanced Interactive Python
```

* Message:

```
This version of python seems to be incorrectly compiled(internal generated filenames are not absolute).This may make the debugger miss breakpoints.Related bug: http://bugs.python.org/issue1666807.
```

For example restart the kernel to see it:

```
Restarting kernel..
```


Discussion:
Hi @James910-code, could you provide more info about your setup? What Spyder version you have installed? Could it be that you updated you anaconda base environment Python version to be 3.11.2 and that started causing the issue?

Without more info there is not much we can due to help, sorry :/ But maybe a look to the Spyder installation guide could help: https://docs.spyder-ide.org/current/installation.html#conda-based-distributions

Let us know if the installation guide helps!
I have the same issue, so I'll include my info here.
Fresh install of spyder via conda:
Env yml was:
```
name: myenv
dependencies:
- python>=3.11
- spyder
- pip
- pip:
  - -r requirements.txt 
```
Generated the environment with:
```
conda env create -f "C:\xxx\my_environment.yml"
```
With no issues.

```
>conda --version
conda 22.9.0
>conda list
...
python                    3.11.3               h966fe2a_0
...
spyder                    5.4.3           py311haa95532_1
spyder-kernels            2.4.3           py311haa95532_0
...
>python --version
Python 3.11.3
```

If I run any command via the IDE i get the warning:
```
In [1]: print('test')
test

This version of python seems to be incorrectly compiled
(internal generated filenames are not absolute).
This may make the debugger miss breakpoints.
Related bug: http://bugs.python.org/issue1666807
```

If I run the same script or command via the python / command line it runs without error.
~~Edit. I see the problem now.  The version of python I have in my 'base' environment is different than the version in my other environments.
If i go to the preferences in spyder, i can change the python interpreter to be the one for that specific environment instead of the default and the error is removed.

I would assume the default interpreter would be the same one as the environment that spyder is being run in.  Why would it be different?~~
See below comment, this doesn't seem to solve the issue for me.
Hi @zach-morris thank you for the info! So you are launching Spyder from the new env and only when setting the custom Python interpreter option to the Python interpreter of the env from where Spyder was launched the message stops appearing?

Maybe something is happening with the activation of the default environment with Python 3.11 (which should be the Python interpreter of the activated env where Spyder was launched as you mentioned) :thinking: What do you think @ccordoba12 ? 
I think we need a more detailed series of steps from @zach-morris (which is what you requested from him) to understand how to reproduce this problem. Otherwise, we won't be able to solve it.
Well, i take that back.  The message is intermittent even if I set the interpreter manually.  I'm unable to determine the cause or a workaround.
Steps are spelled out:
- New install of conda version 22.9.0
- Create new conda environment via (yml is listed above, everything is pretty generic, although I install several packages via pip):
```
conda env create -f "C:\xxx\my_environment.yml"
```
- Start spyder with default settings.  Note that the bottom status bar of spyder denotes the correct environment and python version
- Run any code (i.e print('test')) and recieve the error


The other thing I will say is that I had to uninstall anaconda and re-install the new version recently.  Unsure if there are spyder settings hidden somewhere within windows outside of the anaconda environment that I should purge to see if that is causing the issue.
Could you try to create the env with Python < 3.11 and see if the message still appears? Let us know!
Ok, here are the requested test:

Test Env 1 yml:
```
name: test_env1
dependencies:
- python=3.10
- spyder
- pip
```
Test Env 2 yml:
```
name: test_env2
dependencies:
- python>=3.11
- spyder
- pip
```

Commands run:
```
(base) C:\Users\xxxx>conda env create -f "C:\tools\test_env1.yml"
Collecting package metadata (repodata.json): done
Solving environment: done

Downloading and Extracting Packages
...
Preparing transaction: done
Verifying transaction: done
Executing transaction: done
#
# To activate this environment, use
#
#     $ conda activate test_env1
#
# To deactivate an active environment, use
#
#     $ conda deactivate

Retrieving notices: ...working... done

(base) C:\Users\xxxx>conda env create -f "C:\tools\test_env2.yml"
Collecting package metadata (repodata.json): done
Solving environment: done
Preparing transaction: done
Verifying transaction: done
Executing transaction: done
#
# To activate this environment, use
#
#     $ conda activate test_env2
#
# To deactivate an active environment, use
#
#     $ conda deactivate

Retrieving notices: ...working... done

(base) C:\Users\xxxx>conda activate test_env1

(test_env1) C:\Users\xxxx>spyder

** See screenshot 1 **

(test_env1) C:\Users\xxxx>conda activate test_env2

(test_env2) C:\Users\xxxx>spyder

** See screenshot 2 **
```

Screenshot 1 (python 3.10.11):
![Capture1](https://user-images.githubusercontent.com/12554283/233736968-59c2631b-4393-428d-83fb-74f337b657c5.PNG)

Screenshot 2 (python 3.11.3):
![Capture2](https://user-images.githubusercontent.com/12554283/233736988-74164bab-ff30-4225-8b0e-8d8e555a506a.PNG)


So seems like the issue is related with Python 3.11. Could you set this env variable and check if the message continues?:

    PYDEVD_DISABLE_FILE_VALIDATION=1

Setting that variable and then running the following command in spyder:
```
import os; os.environ.get('PYDEVD_DISABLE_FILE_VALIDATION')
Out[1]: '1'
This version of python seems to be incorrectly compiled
(internal generated filenames are not absolute).
This may make the debugger miss breakpoints.
Related bug: http://bugs.python.org/issue1666807
```
Checked locally and was able to reproduce this :+1: Also, creating the env with packages from the conda-forge channel prevents this message 🤔 

Checking a little bit more the Python issue linked in the message, seems like if you add to the command that Spyder uses to launch the console kernel a `-Xfrozen_modules=off` flag the message stops. The change can be done at: 

https://github.com/spyder-ide/spyder/blob/00ebc4ad55b3c0437f9b096dccfb8dee65033a16/spyder/plugins/ipythonconsole/utils/kernelspec.py#L113-L119

So the code ends up being something like:

```python
            kernel_cmd = [
                pyexec,
                '-Xfrozen_modules=off',
                '-m',
                'spyder_kernels.console',
                '-f',
                '{connection_file}'
            ]
```

The comment that suggest the use of the `-Xfrozen_modules=off` flag: https://bugs.python.org/issue1666807#msg416932 

Confirmed thats a good workaround.  Seems like it would need to be fixed upstream but the upstream issues are closed?  If thats the case, this might need to be permanently added into spyder to avoid the issue.  Related issues:
https://github.com/python/cpython/issues/44604
https://github.com/fabioz/PyDev.Debugger/issues/213
Thank you for the confirmation and extra links @zach-morris ! I think we could try to add the extra flag mentioned checking if the interpreter is running Python 3.11. What do you think @ccordoba12 ? Is there any other way to suppress that message from appearing?
> What do you think @ccordoba12 ?

I think that's a good idea. Thanks for tracking the problem down and finding a solution @dalthviz! Please go ahead and implement it. 

Since Guido himself proposed [the same solution](https://github.com/fabioz/PyDev.Debugger/issues/213#issuecomment-1067214777) in the PyDev.Debugger repo, it doesn't appear to be harmful. In addition, we don't use nor depend on the PyDev debugger, so I'm not expecting major issues with this change.

Resolving patch:
diff --git a/spyder/plugins/ipythonconsole/utils/kernelspec.py b/spyder/plugins/ipythonconsole/utils/kernelspec.py
--- a/spyder/plugins/ipythonconsole/utils/kernelspec.py
+++ b/spyder/plugins/ipythonconsole/utils/kernelspec.py
@@ -112,6 +112,9 @@ def argv(self):
         else:
             kernel_cmd = [
                 pyexec,
+                # This is necessary to avoid a spurious message on Windows.
+                # Fixes spyder-ide/spyder#20800.
+                '-Xfrozen_modules=off',
                 '-m',
                 'spyder_kernels.console',
                 '-f',


Test patch: