Skip to content

Instantly share code, notes, and snippets.

@AJeschor
Last active March 5, 2024 18:10
Show Gist options
  • Save AJeschor/cdb9bef2c0eb7a6230190e8e66fc77e3 to your computer and use it in GitHub Desktop.
Save AJeschor/cdb9bef2c0eb7a6230190e8e66fc77e3 to your computer and use it in GitHub Desktop.
The script detects Poetry, Pipenv, Virtualenv, Conda, and Pyenv environments, initializes variables, checks for package manager/tool existence, determines the active state, and prints name & path info about the active environment. It handles cases of missing tools & provides details on the active Pyenv environment or local environment set by Pyenv.
#!/usr/bin/env python3
# NOTE: If you encounter any issues running the script, update the shebang line
# above to the path of your Python 3 interpreter. You can find the path by running
# the 'which python3' command in your terminal. Example:
# #!/usr/bin/env /usr/bin/python3
"""
This script is designed to assist users in projects where multiple virtual environments coexist within the same directory.
It searches for directories that meet the criteria of a virtual environment and presents the user with them as options in a prompt so they can easily select which environment they wish to activate.
It is most effectively utilized when employed as a sourced script through an alias for seamless integration into your command line environment.
Follow these steps:
1. Save this script to a directory of your choice.
2. Make the script executable:
chmod +x script_name.py
3. Add an alias to your shell with your chosen name. For example, add the following to your shell profile file
(e.g., ~/.bashrc for bash):
alias custom_alias_name='path/to/script_name.py'
Then restart your shell or run source ~/.bashrc.
Now, you can execute the script using your chosen alias.
Now, you can execute the environment check script using your chosen alias.
For instance, if you set the alias as whichenv, you can run: whichenv
"""
# Initialize variables for environment status
poetry_active=false
pipenv_active=false
virtualenv_active=false
conda_active=false
pyenv_active=false
# Function to check if a command exists
command_exists() {
# Check if the command exists
command -v "$1" >/dev/null 2>&1
}
# Check for Poetry environment
if command_exists poetry; then
poetry_active=true
if [ -n "$POETRY_ACTIVE" ]; then
poetry_name=$(basename "$(poetry env info -p)")
echo "Poetry environment is active: $poetry_name"
echo "Path: $(poetry env info -p)"
fi
else
echo "Poetry is not installed"
fi
# Check for Pipenv environment
if command_exists pipenv; then
pipenv_active=true
if [ -n "$PIPENV_ACTIVE" ]; then
pipenv_path=$(pipenv --venv)
if [ -n "$pipenv_path" ]; then
pipenv_name=$(basename "$pipenv_path")
echo "Pipenv is active: $pipenv_name"
echo "Path: $(pipenv --venv)"
virtualenv_active=false
else
echo "Pipenv is active: $PIPENV_ACTIVE"
fi
fi
else
echo "Pipenv is not installed"
fi
# Check for Virtualenv environment only if Poetry or Pipenv is not active
if ! "$poetry_active" && ! "$pipenv_active" && command_exists virtualenv; then
virtualenv_active=true
venv_name=$(basename "$VIRTUAL_ENV")
echo "Virtualenv is active: $venv_name"
echo "Path: $VIRTUAL_ENV"
fi
# Check for Conda environment
if command_exists conda; then
conda_name=$(conda info --env | grep "^*")
if [ -n "$conda_name" ]; then
conda_active=true
echo "Conda environment is active: $conda_name"
echo "Path: $CONDA_PREFIX"
fi
else
echo "Conda is not installed"
fi
# Print statements for non-active environments
if ! "$poetry_active"; then
echo "No Poetry environment active"
fi
if ! "$pipenv_active"; then
echo "No Pipenv environment active"
fi
if ! "$virtualenv_active"; then
echo "No Virtualenv active"
fi
if ! "$conda_active"; then
echo "No Conda environment active"
fi
# Check for Pyenv environment
if command_exists pyenv; then
pyenv_version=$(pyenv version-name 2>/dev/null)
if [ -n "$pyenv_version" ]; then
if [ "$pyenv_version" = "system" ]; then
echo "No Pyenv environment is active (using the system environment)"
else
pyenv_name=$(pyenv version-name)
pyenv_path=$(pyenv prefix)
echo "Pyenv environment is active: $pyenv_name"
echo "Path: $pyenv_path"
fi
elif [ -n "$PYENV_LOCAL_VERSION" ]; then
echo "Pyenv is setting the local environment to: $PYENV_LOCAL_VERSION"
echo "Path: $PYENV_LOCAL_VERSION_PATH"
else
echo "No Pyenv environment is active"
fi
else
echo "Pyenv is not installed"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment