I have a git repo with my project.
I change my conda
environment quite frequently, so I want my repo to track changes in the environment, and to be able to push the most recent one and pull it in another computer. Is it possible?
I search and find several solutions (e.g. https://tdhopper.com/blog/my-python-environment-workflow-with-conda/) but none provide an automatic changes-tracking.
Meaning, I want to include any changes I make in my environment into the project's repository. Like adding new packages etc. So that when I git pull it in another computer, the new package will be also pulled and added to the environment.
Installation of git in conda Step 1: Click here to download the latest version of Anaconda. Step 3: Verify the installation. Step 4: Finally, install git from the anaconda channel.
I use git hooks to make conda environment updates automatic. You can have more information on git hooks here.
The idea here is to have two git hooks:
As described in the documentation, when a git repository is initiated, a folder .git/hooks is created and filled with example scripts. To use one of them, you only have to edit the file, rename it to remove its extension (.sample) and make sure it is executable.
NOTE: I use zsh as shell but the script should be the same in bash (please comment if not), you would just need to change the shebang line.
<ENV_NAME>
by the name of your conda environment):#!/usr/bin/env zsh
echo "\n==================== pre-push hook ===================="
# Export conda environment to yaml file
conda env export -n <ENV_NAME> > env.yml
# Check if new environment file is different from original
git diff --exit-code --quiet env.yml
# If new environment file is different, commit it
if [[ $? -eq 0 ]]; then
echo "Conda environment not changed. No additional commit."
else
echo "Conda environment changed. Commiting new env.yml"
git add env.yml
git commit -m "Updating conda environment"
echo 'You need to push again to push additional "Updating conda environment" commit.'
exit 1
fi
chmod u+x pre-push
)#!/usr/bin/env zsh
echo "\n==================== post-merge hook ===================="
changed_files="$(git diff-tree -r --name-only --no-commit-id ORIG_HEAD HEAD)"
check_run() {
echo "$changed_files" | grep --quiet "$1" && eval "$2"
}
echo "Have to update the conda environment"
check_run env.yml "conda env update --file env.yml"
chmod u+x post-merge
)rebase
for example.prefix
section in the env.yml which give the path to your environment folder on your local machine. After some test, everything seems to run fine but I don't know if this could somehow create conflicts when developing on various machines.So ... comments, corrections and ideas of improvements are more than welcome !
In Conda you can create a virtual environment from and export an environment to a file, which can be included in your git repo. If you pull down your repo on a different machine or delete your environment you can run:
conda env create -f=env.yml
When you make changes to your environment, run an export before you add/commit:
conda env export > env.yml
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With