Repository: spyder-ide/spyder

Issue:
PYTHONPATH environment variable not inherited if using non-default python interpreter
 
## Description
It seems that the iPython console does not inherit `PYTHONPATH` consistently in Spyder.

### What steps will reproduce the problem?
The two scenarios described below illustrate the issue. In both scenarios Spyder is launched from the commandline within an Anaconda environment dedicated for running Spyder. Also, Spyder's PYTHONPATH Manager and the user's `PYTHONPATH` environment variable (defined in ~/.bash_profile) are different.

#### Scenario 1: iPython inherits `PYTHONPATH` from Spyder's PYTHONPATH Manager
1. From the preferences, select "Use the following Python interpreter:". I chose the one from my base Anaconda environment (not the Spyder launch environment).
2. Open a new iPython console
3. Value of `os.environ['PYTHONPATH']` reflects the contents of Spyder's PYTHONPATH Manager, not the user's `PYTHONPATH` environment variable, though other user environment variables defined in ~/.bash_profile are present.

#### Scenario 2: iPython inherits `PYTHONPATH` from user's `PYTHONPATH` environment variable
1. From the preferences, select "Default (i.e. same as Spyder's)"
2. Open a new iPython console
3. Value of `os.environ['PYTHONPATH']` reflects the user's `PYTHONPATH` environment variable.

### Request
Spyder should consistently apply the `PYTHONPATH`, either from the user's environment variables or from Spyder's PYTHONPATH Manager. I'd prefer that the user be able to control which one is used, otherwise default to the user's environment variable.



## Versions

* Spyder version: 3.3.2 
* Python version: 3.7.0
* Qt version: 5.9.6
* PyQt5 version: 5.9.2
* Operating System: Darwin 18.2.0

### Dependencies

```
pyflakes >=0.6.0  :  2.0.0 (OK)
pycodestyle >=2.3 :  2.4.0 (OK)
pygments >=2.0    :  2.2.0 (OK)
sphinx >=0.6.6    :  1.8.1 (OK)
rope >=0.9.4      :  0.11.0 (OK)
jedi >=0.9.0      :  0.13.1 (OK)
psutil >=0.3      :  5.4.8 (OK)
nbconvert >=4.0   :  5.3.1 (OK)
pandas >=0.13.1   :  None (NOK)
numpy >=1.7       :  None (NOK)
sympy >=0.7.3     :  None (NOK)
cython >=0.21     :  None (NOK)
qtconsole >=4.2.0 :  4.4.3 (OK)
IPython >=4.0     :  7.1.1 (OK)
matplotlib >=2.0.0:  None (NOK)
pylint >=0.25     :  2.1.1 (OK)
```



Discussion:

Resolving patch:
diff --git a/spyder/utils/ipython/kernelspec.py b/spyder/utils/ipython/kernelspec.py
--- a/spyder/utils/ipython/kernelspec.py
+++ b/spyder/utils/ipython/kernelspec.py
@@ -76,7 +76,7 @@ def env(self):
         pathlist = CONF.get('main', 'spyder_pythonpath', default=[])
         default_interpreter = CONF.get('main_interpreter', 'default')
         pypath = add_pathlist_to_PYTHONPATH([], pathlist, ipyconsole=True,
-                                            drop_env=(not default_interpreter))
+                                            drop_env=False)
 
         # Environment variables that we need to pass to our sitecustomize
         umr_namelist = CONF.get('main_interpreter', 'umr/namelist')


Test patch:
diff --git a/spyder/utils/ipython/tests/test_spyder_kernel.py b/spyder/utils/ipython/tests/test_spyder_kernel.py
--- a/spyder/utils/ipython/tests/test_spyder_kernel.py
+++ b/spyder/utils/ipython/tests/test_spyder_kernel.py
@@ -12,11 +12,34 @@
 import pytest
 
 from spyder.config.main import CONF
-from spyder.py3compat import PY2, is_binary_string
+from spyder.py3compat import PY2, is_binary_string, to_text_string
 from spyder.utils.encoding import to_fs_from_unicode
 from spyder.utils.ipython.kernelspec import SpyderKernelSpec
 
 
+@pytest.mark.parametrize('default_interpreter', [True, False])
+def test_preserve_pypath(tmpdir, default_interpreter):
+    """
+    Test that we preserve PYTHONPATH in the env vars passed to the kernel
+    when an external interpreter is used or not.
+
+    Regression test for issue 8681.
+    """
+    # Set default interpreter value
+    CONF.set('main_interpreter', 'default', default_interpreter)
+
+    # Add a path to PYTHONPATH env var
+    pypath = to_text_string(tmpdir.mkdir('test-pypath'))
+    os.environ['PYTHONPATH'] = pypath
+
+    # Check that PYTHONPATH is in our kernelspec
+    kernel_spec = SpyderKernelSpec()
+    assert pypath in kernel_spec.env['PYTHONPATH']
+
+    # Restore default value
+    CONF.set('main_interpreter', 'default', True)
+
+
 def test_python_interpreter(tmpdir):
     """Test the validation of the python interpreter."""
     # Set a non existing python interpreter