credit.conda_env
================

.. py:module:: credit.conda_env

.. autoapi-nested-parse::

   Shared conda environment resolution for generated job scripts.

   The PBS (``credit/pbs.py``), Slurm (``credit/slurm.py``), and ``credit`` CLI
   (``credit/cli/_common.py``) launchers all emit shell scripts that must invoke
   ``torchrun`` from a specific conda environment. They previously each carried
   their own copy of the resolution logic, which drifted. This module holds the
   single definition.

   Kept deliberately dependency-free (stdlib only, no ``credit`` imports) so the
   launchers can import it without pulling in the CLI package.



Functions
---------

.. autoapisummary::

   credit.conda_env.conda_prefix_expr
   credit.conda_env.torchrun_path


Module Contents
---------------

.. py:function:: conda_prefix_expr(conda_env: str) -> str

   Return a shell expression that expands to the prefix of *conda_env*.

   A value containing a path separator is already a prefix and is returned as
   is. A bare environment *name* is resolved at run time, never at generation
   time, so it is not mistaken for a same-named directory in the current
   working directory (e.g. the repo's ``credit/`` package dir, which made a
   ``conda: credit`` config yield a bogus ``credit/bin/torchrun``).

   Resolution order in the generated script:

   1. ``$CONDA_PREFIX`` — set by the ``conda activate`` line the scripts emit
      just above this, and correct no matter where the env actually lives.
   2. ``conda info --envs`` lookup by name — covers scripts that never
      activate. This honors ``envs_dirs`` from ``~/.condarc``, unlike
      ``$(conda info --base)/envs/<name>``, which silently produced a
      nonexistent path for envs kept outside the base install (e.g.
      ``/glade/work/$USER/conda-envs``).

   :param conda_env: A conda environment name (e.g. ``"credit"``) or a full
                     environment prefix path (e.g. ``"/glade/work/me/envs/credit"``).

   :returns: A shell expression that expands to the environment prefix.


.. py:function:: torchrun_path(conda_env: str) -> str

   Return a shell expression for ``torchrun`` inside *conda_env*.

   :param conda_env: A conda environment name or full prefix path, as accepted by
                     :func:`conda_prefix_expr`.

   :returns: The ``<prefix>/bin/torchrun`` path, with the prefix resolved at run time
             for a bare environment name.


