From 2d075c17621ab109c535d017d1636024df698b42 Mon Sep 17 00:00:00 2001 From: Carlos Cordoba Date: Fri, 6 Mar 2015 14:17:39 -0500 Subject: [PATCH] Mac app: Make our PYTHONPATH manager work for it Fixes #1321 --- spyderlib/widgets/externalshell/baseshell.py | 4 ++-- spyderlib/widgets/externalshell/pythonshell.py | 12 +++++------- spyderlib/widgets/externalshell/sitecustomize.py | 5 ----- 3 files changed, 7 insertions(+), 14 deletions(-) diff --git a/spyderlib/widgets/externalshell/baseshell.py b/spyderlib/widgets/externalshell/baseshell.py index dca79a19c96..366c9ec6e57 100644 --- a/spyderlib/widgets/externalshell/baseshell.py +++ b/spyderlib/widgets/externalshell/baseshell.py @@ -28,14 +28,14 @@ from spyderlib.py3compat import is_text_string, to_text_string -def add_pathlist_to_PYTHONPATH(env, pathlist): +def add_pathlist_to_PYTHONPATH(env, pathlist, drop_env=False): # PyQt API 1/2 compatibility-related tests: assert isinstance(env, list) assert all([is_text_string(path) for path in env]) pypath = "PYTHONPATH" pathstr = os.pathsep.join(pathlist) - if os.environ.get(pypath) is not None: + if os.environ.get(pypath) is not None and not drop_env: for index, var in enumerate(env[:]): if var.startswith(pypath+'='): env[index] = var.replace(pypath+'=', diff --git a/spyderlib/widgets/externalshell/pythonshell.py b/spyderlib/widgets/externalshell/pythonshell.py index 60e1969f5c3..710b5db9e51 100644 --- a/spyderlib/widgets/externalshell/pythonshell.py +++ b/spyderlib/widgets/externalshell/pythonshell.py @@ -476,10 +476,9 @@ def create_process(self): # IPython kernel env.append('IPYTHON_KERNEL=%r' % self.is_ipykernel) - - pathlist = [] - # Fix encoding with custom "sitecustomize.py" + # Add sitecustomize path to path list + pathlist = [] scpath = osp.dirname(osp.abspath(__file__)) pathlist.append(scpath) @@ -510,8 +509,8 @@ def create_process(self): # 1. PYTHONPATH and PYTHONHOME are set while bootstrapping the app, # but their values are messing sys.path for external interpreters # (e.g. EPD) so we need to remove them from the environment. - # 2. Add this file's dir to PYTHONPATH. This will make every external - # interpreter to use our sitecustomize script. + # 2. Set PYTHONPATH again but without grabbing entries defined in the + # environment (Fixes Issue 1321) # 3. Remove PYTHONOPTIMIZE from env so that we can have assert # statements working with our interpreters (See Issue 1281) if running_in_mac_app(): @@ -520,8 +519,7 @@ def create_process(self): env = [p for p in env if not (p.startswith('PYTHONPATH') or \ p.startswith('PYTHONHOME'))] # 1. - env.append('PYTHONPATH=%s' % osp.dirname(__file__)) # 2. - + add_pathlist_to_PYTHONPATH(env, pathlist, drop_env=True) # 2. env = [p for p in env if not p.startswith('PYTHONOPTIMIZE')] # 3. self.process.setEnvironment(env) diff --git a/spyderlib/widgets/externalshell/sitecustomize.py b/spyderlib/widgets/externalshell/sitecustomize.py index 071ccc0db5d..f0537bc7d42 100644 --- a/spyderlib/widgets/externalshell/sitecustomize.py +++ b/spyderlib/widgets/externalshell/sitecustomize.py @@ -180,11 +180,6 @@ def write(self, text): if MAC_APP_NAME in __file__: interpreter = os.environ.get('SPYDER_INTERPRETER') if MAC_APP_NAME not in interpreter: - # We added this file's dir to PYTHONPATH (in pythonshell.py) - # so that external interpreters can import this script, and - # now we are removing it - del os.environ['PYTHONPATH'] - # Add a minimal library (with spyderlib) at the end of sys.path to # be able to connect our monitor to the external console py_ver = '%s.%s' % (sys.version_info[0], sys.version_info[1])