Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PR: Unify access to current active interpreter #17788

Merged
merged 8 commits into from
May 19, 2022
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -803,7 +803,7 @@ def generate_python_config(self):
environment = None
env_vars = None
else:
environment = self.get_conf('custom_interpreter',
environment = self.get_conf('executable',
section='main_interpreter')
env_vars = os.environ.copy()
# external interpreter should not use internal PYTHONPATH
Expand Down
29 changes: 8 additions & 21 deletions spyder/plugins/maininterpreter/confpage.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,30 +48,17 @@ def __init__(self, plugin, parent):
valid_custom_list.append(path)
self.set_option('custom_interpreters_list', valid_custom_list)

# Python executable selection (initializing default values as well)
# add custom_interpreter to executable selection
executable = self.get_option('executable', get_python_executable())
maurerle marked this conversation as resolved.
Show resolved Hide resolved
if self.get_option('default'):

# check if the executable is valid - use Spyder's if not
if self.get_option('default') or not osp.isfile(executable):
executable = get_python_executable()
elif not self.get_option('custom_interpreter'):
self.set_option('custom_interpreter', ' ')

if not osp.isfile(executable):
# This is absolutely necessary, in case the Python interpreter
# executable has been moved since last Spyder execution (following
# a Python distribution upgrade for example)
self.set_option('executable', get_python_executable())
elif executable.endswith('pythonw.exe'):
# That should not be necessary because this case is already taken
# care of by the `get_python_executable` function but, this was
# implemented too late, so we have to fix it here too, in case
# the Python executable has already been set with pythonw.exe:
self.set_option('executable',
executable.replace("pythonw.exe", "python.exe"))

if not self.get_option('default'):
if not self.get_option('custom_interpreter'):
self.set_option('custom_interpreter', ' ')

plugin._add_to_custom_interpreters(executable)
self.validate_custom_interpreters_list()
plugin._add_to_custom_interpreters(executable)
self.validate_custom_interpreters_list()

def initialize(self):
super().initialize()
Expand Down
33 changes: 15 additions & 18 deletions spyder/plugins/maininterpreter/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,31 +53,28 @@ def update_actions(self):

@on_conf_change(option=['default', 'custom_interpreter', 'custom'])
def section_conf_update(self, option, value):
if option in ['default', 'custom_interpreter', 'custom'] and value:
self._update_status()
self.sig_interpreter_changed.emit()

# Set new interpreter
if ((option == 'default' and value) or
(option == 'custom' and not value)):
executable = get_python_executable()
else:
executable = osp.normpath(self.get_conf('custom_interpreter'))
if (option in ['custom', 'custom_interpreter'] and
osp.isfile(executable)):
self.sig_add_to_custom_interpreters_requested.emit(executable)
self.sig_add_to_custom_interpreters_requested.emit(executable)
# Setting executable option that will be used by other plugins in Spyder.
maurerle marked this conversation as resolved.
Show resolved Hide resolved
if executable != self.get_conf('executable'):
self.set_conf('executable', executable)
maurerle marked this conversation as resolved.
Show resolved Hide resolved

@on_conf_change(option=['executable'])
def on_executable_changed(self, value):
# announce update
self._update_status()
self.sig_interpreter_changed.emit()

def on_close(self):
self.interpreter_status.close()

# ---- Public API
def get_main_interpreter(self):
if self.get_conf('default'):
return get_python_executable()
else:
executable = osp.normpath(self.get_conf('custom_interpreter'))

# Check if custom interpreter is still present
if osp.isfile(executable):
return executable
else:
return get_python_executable()
return self.get_conf('executable', get_python_executable())

# ---- Private API
def _update_status(self):
Expand Down
7 changes: 0 additions & 7 deletions spyder/plugins/maininterpreter/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,6 @@ def on_statusbar_teardown(self):
statusbar = self.get_plugin(Plugins.StatusBar)
statusbar.remove_status_widget(self.interpreter_status.ID)

# ---- Public API
def get_interpreter(self):
"""Get current interpreter."""
container = self.get_container()
return container.get_main_interpreter()

@property
def interpreter_status(self):
return self.get_container().interpreter_status
Expand All @@ -126,4 +120,3 @@ def _add_to_custom_interpreters(self, interpreter):
if interpreter not in custom_list:
custom_list.append(interpreter)
self.set_conf('custom_interpreters_list', custom_list)
self.set_conf('executable', interpreter)
5 changes: 3 additions & 2 deletions spyder/plugins/maininterpreter/widgets/status.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
from spyder.api.translations import get_translation
from spyder.api.widgets.status import BaseTimerStatus
from spyder.utils.conda import get_list_conda_envs
from spyder.utils.misc import get_python_executable
from spyder.utils.programs import get_interpreter_info
from spyder.utils.pyenv import get_list_pyenv_envs
from spyder.utils.workers import WorkerManager
Expand Down Expand Up @@ -73,15 +74,15 @@ def get_value(self):
# Env was removed on Mac or Linux
self.set_conf('custom', False)
self.set_conf('default', True)
self.update_interpreter(sys.executable)
self.update_interpreter(get_python_executable())
elif not osp.isfile(self._interpreter):
# This can happen on Windows because the interpreter was
# renamed to .conda_trash
if not osp.isdir(osp.join(env_dir, 'conda-meta')):
# If conda-meta is missing, it means the env was removed
self.set_conf('custom', False)
self.set_conf('default', True)
self.update_interpreter(sys.executable)
self.update_interpreter(get_python_executable())
else:
# If not, it means the interpreter is being updated so
# we need to update its version
Expand Down
5 changes: 1 addition & 4 deletions spyder/plugins/profiler/widgets/main_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -560,10 +560,7 @@ def start(self, wdir=None, args=None, pythonpath=None):
if args:
p_args.extend(shell_split(args))

executable = sys.executable
if executable.endswith("spyder.exe"):
# py2exe distribution
executable = "python.exe"
executable = self.get_conf('executable', section='main_interpreter')
ccordoba12 marked this conversation as resolved.
Show resolved Hide resolved

self.process.start(executable, p_args)
running = self.process.waitForStarted()
Expand Down
2 changes: 1 addition & 1 deletion spyder/utils/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ def get_error_match(text):


def get_python_executable():
"""Return path to Python executable"""
"""Return path to Spyder Python executable"""
executable = sys.executable.replace("pythonw.exe", "python.exe")
if executable.endswith("spyder.exe"):
# py2exe distribution
Expand Down