Created
March 30, 2023 22:55
-
-
Save tom-pollak/8326cb9b9989e0326f0d2e19fba6aeb0 to your computer and use it in GitHub Desktop.
Activate python env automatically with direnv
This file contains 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
layout_poetry() { | |
PYPROJECT_TOML="${PYPROJECT_TOML:-pyproject.toml}" | |
if [[ ! -f "$PYPROJECT_TOML" ]]; then | |
log_status "No pyproject.toml found. Executing \`poetry init\` to create a \`$PYPROJECT_TOML\` first." | |
poetry init | |
fi | |
VIRTUAL_ENV=$(poetry env info --path 2>/dev/null ; true) | |
if [[ -z $VIRTUAL_ENV || ! -d $VIRTUAL_ENV ]]; then | |
log_status "No virtual environment exists. Executing \`poetry install\` to create one." | |
poetry install | |
VIRTUAL_ENV=$(poetry env info --path) | |
fi | |
PATH_add "$VIRTUAL_ENV/bin" | |
export POETRY_ACTIVE=1 | |
export VIRTUAL_ENV | |
} | |
layout_venv() { | |
if [[ ! -d "$VENV_PATH" ]]; then | |
log_status "No venv found, creating venv at: $VENV_PATH" | |
python -m venv "$VENV_PATH" | |
fi | |
PATH_add "$VENV_PATH/bin" | |
export VENV_ACTIVE=1 | |
export VIRTUAL_ENV="$VENV_PATH" | |
} |
This file contains 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
#!/usr/bin/env bash | |
echo "layout poetry" >.envrc | |
direnv allow . | |
direnv exec . echo |
This file contains 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
#!/usr/bin/env bash | |
VENV_PATH=${1:-".venv"} | |
echo export VENV_PATH=$VENV_PATH > .envrc | |
echo "layout venv" >> .envrc | |
direnv allow . | |
direnv exec . echo |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment