Skip to content

Instantly share code, notes, and snippets.

@JeffJacobson
Created April 9, 2026 16:09
Show Gist options
  • Select an option

  • Save JeffJacobson/da3d4902188d878d483179d7692f9a7d to your computer and use it in GitHub Desktop.

Select an option

Save JeffJacobson/da3d4902188d878d483179d7692f9a7d to your computer and use it in GitHub Desktop.
Setup conda prompt for local environment
"""
Creates PowerShell scripts to modify the conda environment indicator at
the command prompt, so it doesn't display the lengthy absolute path of
the environment.
"""
from pathlib import Path, PurePath
from sys import stderr
from typing import Annotated
import typer
from typer import echo, secho
app = typer.Typer()
_PWSH_SCRIPTS = {
PurePath("activate.d", "set-prompt.ps1"): """
# save old modifier if present
if ($env:CONDA_PROMPT_MODIFIER) {
$env:_OLD_CONDA_PROMPT_MODIFIER = $env:CONDA_PROMPT_MODIFIER
}
$env:CONDA_PROMPT_MODIFIER = '(.conda) '
""",
PurePath("deactivate.d", "unset-prompt.ps1"): """
if ($env:_OLD_CONDA_PROMPT_MODIFIER) {
$env:CONDA_PROMPT_MODIFIER = $env:_OLD_CONDA_PROMPT_MODIFIER
Remove-Item Env:_OLD_CONDA_PROMPT_MODIFIER
} else {
Remove-Item Env:CONDA_PROMPT_MODIFIER -ErrorAction SilentlyContinue
}
""",
}
"""A dictionary keyed by PurePath of a PowerShel script file,
with a value of the text to be written to it.
"""
def enumerate_pwsh_scripts(root: Path):
"""Enumerate through the _PWSH_SCRIPTS dict
Parameters:
root: The path of the conda environment directory.
Yields:
A tuple: a Path and a str containing the content to be written to that path.
Raises:
Raises a FileNotFoundError if `root` does not exist.
"""
if not root.exists():
raise FileNotFoundError(root)
for pwsh_path, pwsh_script in _PWSH_SCRIPTS.items():
yield root / pwsh_path, pwsh_script
def env_dir_default_factory():
"""Recursively searches for a directory named "activate.d"
and returns the first matching item's parent directory,
or `None` if no match was found.
"""
here = Path(".")
for p in (p.parent for p in here.rglob("activate.d") if p.is_dir()):
return p
@app.command(help=__doc__)
def create_prompt_scripts(
env_dir: Annotated[
Path,
typer.Argument(
dir_okay=True,
file_okay=False,
exists=True,
help="Location of the Conda environment",
default_factory=env_dir_default_factory,
),
],
overwrite: Annotated[
bool, typer.Option(help="Overwrite the PowerShell script files if they exist")
] = False,
):
"""Creates PowerShell scripts to modify the conda environment
indicator at the command prompt, so it doesn't display the
lengthy absolute path of the environment.
Parameters:
env_dir: Conda environment directory
overwrite: Set to True to overwrite existing files.
"""
for p, s in enumerate_pwsh_scripts(env_dir):
# Skip existing files unless `overwrite` is True.
if p.exists():
if not overwrite:
secho(
f"{p} already exists. Skipping creation.",
stderr,
fg=typer.colors.YELLOW,
)
continue
echo(f"Overwriting {p}…", file=stderr)
else:
echo(f"Creating {p}…", file=stderr)
# Write the script contents to the file.
with p.open("w") as f:
f.write(s)
secho(f"Created {p}.", file=stderr, fg=typer.colors.GREEN)
if __name__ == "__main__":
app()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment