Repository: spyder-ide/spyder

Issue:
Fatal Python error when running profiler in macOS app with external environment
## Description

### What steps will reproduce the problem?

<!--- You can use Markdown here --->

Attempting to run the profiler on a simple test script in the macOS app results in the following error:

<img width="654" alt="Screen Shot 2022-05-25 at 10 46 32 AM" src="https://user-images.githubusercontent.com/9618975/170330955-3cc2535b-b3dd-4299-88a0-4bc6704c318a.png">

This only seems to occur when using an external environment. The profiler runs as expected when using the "Same as Spyder" environment, as well as for bootstrap with "Same as Spyder" _and_ external environment.



## Versions

* Spyder version: 5.3.1 
* Python version: 3.9.5
* Qt version: 5.15.2
* PyQt5 version: 5.15.6
* Operating System: Darwin 20.6.0

### Dependencies

```
# Mandatory:
applaunchservices >=0.1.7            :  0.3.0 (OK)
atomicwrites >=1.2.0                 :  1.4.0 (OK)
chardet >=2.0.0                      :  4.0.0 (OK)
cloudpickle >=0.5.0                  :  2.1.0 (OK)
cookiecutter >=1.6.0                 :  1.7.3 (OK)
diff_match_patch >=20181111          :  20200713 (OK)
intervaltree >=3.0.2                 :  3.1.0 (OK)
IPython >=7.31.1;<8.0.0              :  7.33.0 (OK)
jedi >=0.17.2;<0.19.0                :  0.18.1 (OK)
jellyfish >=0.7                      :  0.9.0 (OK)
jsonschema >=3.2.0                   :  4.5.1 (OK)
keyring >=17.0.0                     :  23.5.1 (OK)
nbconvert >=4.0                      :  6.5.0 (OK)
numpydoc >=0.6.0                     :  1.3.1 (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.1 (OK)
pygments >=2.0                       :  2.12.0 (OK)
pylint >=2.5.0                       :  2.13.9 (OK)
pyls_spyder >=0.4.0                  :  0.4.0 (OK)
pylsp >=1.4.1;<1.5.0                 :  1.4.1 (OK)
pylsp_black >=1.2.0                  :  1.2.1 (OK)
qdarkstyle >=3.0.2;<3.1.0            :  3.0.3 (OK)
qstylizer >=0.1.10                   :  0.2.1 (OK)
qtawesome >=1.0.2                    :  1.1.1 (OK)
qtconsole >=5.3.0;<5.4.0             :  5.3.0 (OK)
qtpy >=2.1.0                         :  2.1.0 (OK)
rtree >=0.9.7                        :  1.0.0 (OK)
setuptools >=49.6.0                  :  62.3.2 (OK)
sphinx >=0.6.6                       :  4.5.0 (OK)
spyder_kernels >=2.3.1;<2.4.0        :  2.3.1 (OK)
textdistance >=4.2.0                 :  4.2.2 (OK)
three_merge >=0.1.1                  :  0.1.1 (OK)
watchdog >=0.10.3                    :  2.1.8 (OK)
zmq >=22.1.0                         :  23.0.0 (OK)

# Optional:
cython >=0.21                        :  0.29.30 (OK)
matplotlib >=3.0.0                   :  3.5.2 (OK)
numpy >=1.7                          :  1.22.4 (OK)
pandas >=1.1.1                       :  1.4.2 (OK)
scipy >=0.17.0                       :  1.8.1 (OK)
sympy >=0.7.3                        :  1.10.1 (OK)

# Spyder plugins:
spyder_terminal.terminalplugin 1.2.2 :  1.2.2 (OK)
```



Discussion:

Resolving patch:
diff --git a/spyder/plugins/profiler/widgets/main_widget.py b/spyder/plugins/profiler/widgets/main_widget.py
--- a/spyder/plugins/profiler/widgets/main_widget.py
+++ b/spyder/plugins/profiler/widgets/main_widget.py
@@ -34,7 +34,7 @@
 from spyder.api.translations import get_translation
 from spyder.api.widgets.main_widget import PluginMainWidget
 from spyder.api.widgets.mixins import SpyderWidgetMixin
-from spyder.config.base import get_conf_path
+from spyder.config.base import get_conf_path, running_in_mac_app
 from spyder.plugins.variableexplorer.widgets.texteditor import TextEditor
 from spyder.py3compat import to_text_string
 from spyder.utils.misc import get_python_executable, getcwd_or_home
@@ -543,6 +543,13 @@ def start(self, wdir=None, args=None, pythonpath=None):
             proc_env.insert('PYTHONPATH', os.pathsep.join(pythonpath))
         self.process.setProcessEnvironment(proc_env)
 
+        executable = self.get_conf('executable', section='main_interpreter')
+
+        if not running_in_mac_app(executable):
+            env = self.process.processEnvironment()
+            env.remove('PYTHONHOME')
+            self.process.setProcessEnvironment(env)
+
         self.output = ''
         self.error_output = ''
         self.running = True
@@ -560,8 +567,6 @@ def start(self, wdir=None, args=None, pythonpath=None):
         if args:
             p_args.extend(shell_split(args))
 
-        executable = self.get_conf('executable', section='main_interpreter')
-
         self.process.start(executable, p_args)
         running = self.process.waitForStarted()
         if not running:


Test patch: