|
| 1 | +#!/bin/bash |
| 2 | +# This helper script creates a conda environment. |
| 3 | +# You should not run this directly. Instead, use build_agent_conda_env.sh or build_gym_conda_env.sh. |
| 4 | + |
| 5 | +set -euo pipefail |
| 6 | + |
| 7 | +# 1. Checks. |
| 8 | +# 1.1. Check that conda is installed. |
| 9 | +if ! command -v conda &> /dev/null; then |
| 10 | + echo "Error: Conda is not installed" |
| 11 | + exit 1 |
| 12 | +fi |
| 13 | + |
| 14 | +# 1.2. Input validation. |
| 15 | +if [ "$#" -lt 3 ]; then |
| 16 | + echo "Usage: ./_build_conda_env.sh <env_name> <python_version_path> <requirements_path>" |
| 17 | + exit 1 |
| 18 | +fi |
| 19 | + |
| 20 | +env_name=$1 |
| 21 | +python_version_path=$2 |
| 22 | +requirements_path=$3 |
| 23 | + |
| 24 | +# 1.3. Check that the environment doesn't already exist. |
| 25 | +if conda info --envs | grep -q "^$env_name "; then |
| 26 | + echo "Error: Conda environment '$env_name' already exists" |
| 27 | + exit 1 |
| 28 | +fi |
| 29 | + |
| 30 | +# 2. Set up the environment. |
| 31 | +# Note: I am intentionally not using environment.yml. I am instead using |
| 32 | +# requirements.txt and .python_version. This is for two reasons: |
| 33 | +# 1. environment.yml sets the conda env name. However, I want to enforce |
| 34 | +# that the conda env name is the same as the agent name. |
| 35 | +# 2. requirements.txt can be used by pip and only contains packages and |
| 36 | +# not any additional conda-specific syntax, making it more modular |
| 37 | +# and flexible. |
| 38 | + |
| 39 | +# 2.1. Set python_version variable. |
| 40 | +if [ -f "$python_version_path" ]; then |
| 41 | + python_version=$(cat "$python_version_path") |
| 42 | +else |
| 43 | + echo "Warning: .python_version not found in $python_version_path. Using default Python 3.10." |
| 44 | + python_version="3.10" |
| 45 | +fi |
| 46 | + |
| 47 | +# 2.2. Create conda environment with specified Python version. |
| 48 | +echo "Creating conda environment '$env_name' with Python $python_version..." |
| 49 | +eval "$(conda shell.bash hook)" |
| 50 | +conda create -y -n "$env_name" python="$python_version" |
| 51 | + |
| 52 | +# 2.3. Install the packages. |
| 53 | +conda activate "$env_name" |
| 54 | + |
| 55 | +if [ -f "$requirements_path" ]; then |
| 56 | + echo "Installing pip requirements from $requirements_path..." |
| 57 | + pip install -r "$requirements_path" |
| 58 | +else |
| 59 | + echo "Warning: $requirements_path not found. Skipping pip install." |
| 60 | +fi |
| 61 | + |
| 62 | +# We always install gymlib so that the agent has access to it. |
| 63 | +if [ -d "gymlib" ]; then |
| 64 | + echo "Installing gymlib in editable mode..." |
| 65 | + pip install -e ./gymlib |
| 66 | +else |
| 67 | + echo "Error: gymlib directory not found in $(pwd). Please ensure you're running this script from the right folder." |
| 68 | + exit 1 |
| 69 | +fi |
| 70 | + |
| 71 | +conda deactivate |
| 72 | + |
| 73 | +# 2.4. Success message. |
| 74 | +echo "Conda environment '$env_name' created successfully." |
| 75 | +echo "It is not currently activated. To activate it, run 'conda activate $env_name'." |
0 commit comments