Skip to content

Commit 0d8bf4d

Browse files
authored
Merge pull request #63 from molssi-seamm/dev
Better handling of Conda paths
2 parents b8adddf + 196b88d commit 0d8bf4d

File tree

2 files changed

+33
-14
lines changed

2 files changed

+33
-14
lines changed

HISTORY.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
=======
22
History
33
=======
4+
2025.6.6 -- Better handling of Conda paths
5+
* Some conda installations use paths rather than names. This may be common at
6+
computer centers with a central installation of the base environment and
7+
individual user installations of other environments. This version supports such
8+
installations.
9+
410
2025.4.30 -- Bugfix: Issues in graphical install
511
* Also added 'conda list --explicit' environment files for each environment.
612

seamm_installer/conda.py

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ def environments(self):
5656
for env in self._data["envs"]:
5757
path = Path(env)
5858
self.logger.debug(f" environment {env}")
59-
if path == self.root_path:
59+
if path == self.prefix:
6060
result.append("base")
6161
self.logger.debug(" --> base")
6262
else:
@@ -171,7 +171,9 @@ def _initialize(self):
171171
# break
172172

173173
# self.root_path = root
174-
self.root_path = Path(self._data["conda_prefix"])
174+
self.root_path = Path(self._data["active_prefix"]).parent
175+
if self.root_path.name == "envs":
176+
self.root_path = self.root_path.parent
175177

176178
tmp = "\n\t".join(self.environments)
177179
self.logger.info(f"environments:\n\t{tmp}")
@@ -199,8 +201,7 @@ def create_environment(self, environment_file, name=None, force=False):
199201
command += " --force"
200202
if name is not None:
201203
# Using the name leads to odd paths, so be explicit.
202-
# command += f" --name '{name}'"
203-
path = self.root_path / "envs" / name
204+
path = self._resolve_environment_path(name)
204205
command += f" --prefix '{str(path)}'"
205206
self.logger.debug(f"command = {command}")
206207
try:
@@ -221,7 +222,7 @@ def delete_environment(self, name):
221222
The name of the environment.
222223
"""
223224
# Using the name leads to odd paths, so be explicit.
224-
path = self.root_path / "envs" / name
225+
path = self._resolve_environment_path(name)
225226

226227
command = f"conda env remove --yes --prefix '{str(path)}'"
227228

@@ -260,7 +261,8 @@ def export_environment(self, environment, path=None):
260261
path : str or pathlib.Path = None
261262
An optional filename to export to
262263
"""
263-
command = f"conda env export --name '{environment}'"
264+
environment_path = self._resolve_environment_path(environment)
265+
command = f"conda env export --prefix '{environment_path}'"
264266
if path is not None:
265267
command += f" --file '{path}'"
266268
try:
@@ -303,7 +305,7 @@ def install(
303305
if environment is not None:
304306
# Using the name leads to odd paths, so be explicit.
305307
# command += f" --name '{environment}'"
306-
path = self.root_path / "envs" / environment
308+
path = self._resolve_environment_path(environment)
307309
command += f" --prefix '{str(path)}'"
308310
if override_channels:
309311
command += " --override-channels"
@@ -355,7 +357,8 @@ def list(
355357
else:
356358
command = "conda list --json"
357359
if environment is not None:
358-
command += f" --name '{environment}'"
360+
path = self._resolve_environment_path(environment)
361+
command += f" --prefix '{path}'"
359362
if fullname:
360363
command += " --full-name"
361364
if query is not None:
@@ -400,10 +403,10 @@ def path(self, environment):
400403
The path to the environment.
401404
"""
402405
if environment == "base":
403-
return Path(self.root_path)
406+
return Path(self.prefix)
404407
else:
405408
for env in self._data["envs"]:
406-
if env != self.root_path:
409+
if env != self.prefix:
407410
path = Path(env)
408411
if environment == path.name:
409412
return path
@@ -417,7 +420,8 @@ def remove_environment(self, environment):
417420
environment : str
418421
The name of the environment to remove.
419422
"""
420-
command = f"conda env remove --name '{environment}' --yes --json"
423+
path = self._resolve_environment_path(environment)
424+
command = f"conda env remove --prefix '{path}' --yes --json"
421425
try:
422426
self._execute(command)
423427
except subprocess.CalledProcessError as e:
@@ -547,7 +551,7 @@ def update(
547551
if environment is not None:
548552
# Using the name leads to odd paths, so be explicit.
549553
# command += f" --name '{environment}'"
550-
path = self.root_path / "envs" / environment
554+
path = self._resolve_environment_path(environment)
551555
command += f" --prefix '{str(path)}'"
552556
if override_channels:
553557
command += " --override-channels"
@@ -602,7 +606,8 @@ def uninstall(
602606
"""
603607
command = "conda uninstall --yes "
604608
if environment is not None:
605-
command += f" --name '{environment}'"
609+
path = self._resolve_environment_path(environment)
610+
command += f" --prefix '{path}'"
606611
if override_channels:
607612
command += " --override-channels"
608613
if channels is None:
@@ -639,7 +644,7 @@ def update_environment(self, environment_file, name=None):
639644
if name is not None:
640645
# Using the name leads to odd paths, so be explicit.
641646
# command += f" --name '{name}'"
642-
path = self.root_path / "envs" / name
647+
path = self._resolve_environment_path(name)
643648
command += f" --prefix '{str(path)}'"
644649
print(f"command = {command}")
645650
self.logger.debug(f"command = {command}")
@@ -712,3 +717,11 @@ def _execute(
712717
if update is None:
713718
print("")
714719
return result, stdout, stderr
720+
721+
def _resolve_environment_path(self, pathname):
722+
"""Get the path given either a name or path for an environment."""
723+
if Path(pathname).is_absolute:
724+
path = Path(pathname)
725+
else:
726+
path = self.root_path / "envs" / pathname
727+
return path

0 commit comments

Comments
 (0)