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: Show environments with the same name in different paths in the New console in environment menu #23632

Merged
merged 21 commits into from
Feb 14, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
11 changes: 8 additions & 3 deletions spyder/plugins/ipythonconsole/widgets/main_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
from jupyter_core.paths import jupyter_config_dir
import qstylizer.style
from qtconsole.svg import save_svg, svg_to_clipboard
from qtpy.QtCore import Signal, Slot
from qtpy.QtGui import QColor, QKeySequence
from qtpy.QtCore import Qt, Signal, Slot
from qtpy.QtGui import QColor, QFontMetrics, QKeySequence
from qtpy.QtPrintSupport import QPrintDialog, QPrinter
from qtpy.QtWidgets import (
QApplication, QHBoxLayout, QLabel, QMessageBox, QVBoxLayout, QWidget)
Expand Down Expand Up @@ -1202,9 +1202,13 @@ def _update_environment_menu(self):
for env_key, env_info in self.envs.items():
env_name = env_key.split()[-1]
path_to_interpreter, python_version = env_info
fm = QFontMetrics(self.font())
env_elided = fm.elidedText(
env_key, Qt.ElideMiddle, 250
)

# Text for actions
text = f"{env_key} ({python_version})"
text = f"{env_elided} ({python_version})"

# Change text in case env is the default or internal (i.e. same as
# Spyder) one
Expand Down Expand Up @@ -1235,6 +1239,7 @@ def _update_environment_menu(self):
),
overwrite=True,
register_action=False,
tip=text,
)

# Add default env as the first entry in the menu
Expand Down
29 changes: 28 additions & 1 deletion spyder/utils/conda.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,8 @@ def get_list_conda_envs():
out = {'envs': []}

for env in out['envs']:
name = env.split(osp.sep)[-1]
data = env.split(osp.sep)
name = data[-1]
path = osp.join(env, 'python.exe') if WINDOWS else osp.join(
env, 'bin', 'python')

Expand All @@ -141,6 +142,32 @@ def get_list_conda_envs():
name = ('base' if name.lower().startswith('anaconda') or
name.lower().startswith('miniconda') else name)
name = 'Conda: {}'.format(name)

if name in env_list:
if not (path, version.strip()) == env_list[name]:
prev_info = env_list[name]
prev_data = prev_info[0]
prev_data = prev_data.split(osp.sep)
env_list.pop(name)
index_common_folder = 1
end_path = 1
if not WINDOWS:
end_path = 2
for i in range(-1, -len(data) - 1, -1):
if data[i] == prev_data[i - end_path]:
index_common_folder += 1
else:
break
path_part = prev_data[
-index_common_folder - end_path : -end_path
]
prev_name = (
f'Conda: '
f'{"/".join(path_part)}'
)
env_list[prev_name] = prev_info
name = f'Conda: {"/".join(data[-index_common_folder:])}'

env_list[name] = (path, version.strip())

CONDA_ENV_LIST_CACHE = env_list
Expand Down
19 changes: 19 additions & 0 deletions spyder/utils/pyenv.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,25 @@ def get_list_pyenv_envs():

name = f'Pyenv: {data[-1]}'
version = f'Python {data[0]}'

if name in env_list:
if not (path, version) == env_list[name]:
prev_info = env_list[name]
prev_data = prev_info[0]
prev_data = prev_data.split(osp.sep)
env_list.pop(name)
index_common_folder = 1
for i in range(-1, -len(data) - 1, -1):
if data[i] == prev_data[i]:
index_common_folder += 1
else:
break
part_path = prev_data[-index_common_folder : -1]
prev_name = f'Pyenv: {"/".join(part_path)}'
env_list[prev_name] = prev_info
name = f'Pyenv: {"/".join(data[-index_common_folder:])}'


env_list[name] = (path, version)

PYENV_ENV_LIST_CACHE = env_list
Expand Down
12 changes: 11 additions & 1 deletion spyder/utils/tests/test_conda.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,17 @@ def test_find_conda():
def test_get_list_conda_envs():
output = get_list_conda_envs()

expected_envs = ['base', 'jedi-test-env', 'spytest-ž', 'test']
if sys.platform == 'darwin':
expected_envs = [
'base',
'jedi-test-env',
'spytest-ž',
'micromamba/envs/test',
'miniconda3/envs/test'
]
else:
expected_envs = ['base', 'jedi-test-env', 'spytest-ž', 'test']

expected_envs = ['Conda: ' + env for env in expected_envs]

assert set(expected_envs) == set(output.keys())
Expand Down
Loading