Skip to content

Instantly share code, notes, and snippets.

@GioBonvi
Last active November 23, 2021 23:50
Show Gist options
  • Save GioBonvi/0f864717e415bf5bae1ae6516df3b34c to your computer and use it in GitHub Desktop.
Save GioBonvi/0f864717e415bf5bae1ae6516df3b34c to your computer and use it in GitHub Desktop.
A bash script to run a python command in a given conda environment.
#!/bin/bash
# Run a python command in a given conda environment.
#
# Usage:
#
# python_conda.sh ENV_NAME FOLDER COMMAND ARGUMENTS
#
# Examples:
#
# Run the interactive shell - python
# python_conda.sh my_env
#
# Run a single python command - python -c print("Hello world!")
# python_conda.sh my_env -c 'print("Hello world!")'
#
# Source: https://gist.github.com/GioBonvi/0f864717e415bf5bae1ae6516df3b34c
# Adapt according to your installation.
CONDA_BASE_DIR=~/miniconda3/
cd "$2"
# Activate the conda environment.
source "$CONDA_BASE_DIR/etc/profile.d/conda.sh"
conda activate "$1"
# Execute the python command.
python "$3" "${@:4}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment