How to run a command in a Conda environment without activating it

Automation often needs one command from a Conda environment without turning the whole shell, cron job, or CI step into an activated session. conda run starts that child process with the selected environment's paths and variables, then returns control to the same parent shell.

Conda can target an environment by name with -n or by full prefix path with -p. The command after the environment selector is the executable to run, and a -- separator keeps executable options from being read as conda run options.

Use --cwd when the child process must start from a project directory, and use --live-stream or --no-capture-output when logs should appear as the command runs. The target environment must already exist; create or repair it separately if conda run reports that the environment cannot be found.

Steps to run a command in a Conda environment without activating it:

  1. List available Conda environments and choose the target name.
    $ conda env list
     
    # conda environments:
    #
    # * -> active
    # + -> frozen
    base                     /opt/conda
    analytics                /opt/conda/envs/analytics

    The example uses an environment named analytics. Use the exact name from your own environment list, or use -p with a full prefix path when names are ambiguous.

  2. Check the current shell before running the isolated command.
    $ echo "${CONDA_DEFAULT_ENV:-not active}"
    not active

    This shell variable is only a quick before-and-after marker. An already activated shell should show the same value again after conda run exits.

  3. Run the command inside the target environment by name.
    $ conda run -n analytics python -c "print(__import__('sys').executable)"
    /opt/conda/envs/analytics/bin/python

    The executable path should point inside the selected environment, not the parent shell's Python path.

  4. Add -- before the executable when passing options to that executable.
    $ conda run -n analytics -- python -V
    Python 3.13.14

    The separator is useful when the command after conda run has its own flags or when a script generator builds the command line from separate pieces.

  5. Set the child command's working directory when relative paths matter.
    $ conda run -n analytics --cwd /srv/analytics python -c "from pathlib import Path; print(Path.cwd())"
    /srv/analytics

    Replace /srv/analytics with the project or job directory that the command expects.

  6. Stream command output when automation needs logs immediately.
    $ conda run --live-stream -n analytics python -c "print(*range(2), sep=chr(10))"
    0
    1

    --live-stream and --no-capture-output both disable Conda's stdout and stderr capture for the child process.

  7. Confirm the parent shell did not switch environments.
    $ echo "${CONDA_DEFAULT_ENV:-not active}"
    not active

    If the value changed, another shell command changed the session; conda run itself exits after the child process and does not leave the selected environment activated.