Last active
January 8, 2024 08:48
-
-
Save sizhky/ec7bbebd65e6eaf84e07bb88c493997d to your computer and use it in GitHub Desktop.
utilities for conda environments
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
create_conda_env() { | |
# Check if the environment name and Python version are provided as arguments | |
if [ $# -lt 2 ]; then | |
echo "Please provide the conda environment name and Python version as arguments." | |
return 1 | |
fi | |
# Create conda environment with the specified Python version | |
conda create -n "$1" "$2" | |
# Activate conda environment | |
conda activate "$1" | |
# Install Jupyter and IPython kernel | |
conda install jupyter ipykernel | |
# Register the environment as an IPython kernel using sudo | |
sudo $(which python) -m ipykernel install --name="$1" | |
echo "Conda environment setup completed." | |
} | |
purge_conda_env() { | |
# Check if the environment name is provided as an argument | |
if [ $# -eq 0 ]; then | |
echo "Please provide the conda environment name as an argument." | |
return 1 | |
fi | |
# Get the current size of the environment | |
environment_path=$(conda info --envs | grep -F "$1" | awk '{print $2}') | |
if [ -z "$environment_path" ]; then | |
echo "Conda environment '$1' not found." | |
return 1 | |
fi | |
initial_size=$(du -sh "$environment_path" | awk '{print $1}') | |
# Deactivate the environment if currently activated | |
conda deactivate | |
# Remove the environment | |
conda env remove -n "$1" | |
# Get the size after removal | |
final_size=$(du -sh "$environment_path" 2>/dev/null | awk '{print $1}') | |
if [ -z "$final_size" ]; then | |
echo "Failed to calculate the size of the deleted environment." | |
else | |
echo "Conda environment '$1' has been deleted and purged." | |
echo "Disk space cleaned/utilized: $((initial_size - final_size))" # in kilobytes | |
fi | |
} | |
#!/bin/bash | |
show_conda_envs_diskspace() { | |
# Get a list of all Conda environments | |
envs=$(conda info --envs | grep "^.*\/" | awk '{print $1}') | |
if [ -z "$envs" ]; then | |
echo "No Conda environments found." | |
return 1 | |
fi | |
# Loop through each environment and display its disk space usage | |
for env in $envs; do | |
env_path=$(echo "$env" | awk -F'/' '{print $NF}') | |
env_size=$(du -sh "$env" | awk '{print $1}') | |
echo "Environment: $env_path" | |
echo "Disk space usage: $env_size" | |
echo "--------------------" | |
done | |
} | |
purge_conda_envs() { | |
# Check if at least one environment name is provided as an argument | |
if [ $# -eq 0 ]; then | |
echo "Please provide at least one conda environment name as an argument." | |
return 1 | |
fi | |
for env_name in "$@"; do | |
purge_conda_env "$env_name" | |
done | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment