Last active
June 14, 2023 16:56
-
-
Save FlipperPA/4f4de8e1dcfe0d9a6f11171b733c03b7 to your computer and use it in GitHub Desktop.
Shortcuts for Python 3's venv for virtualenvwrapper users.
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
export VENV_HOME=~/.venvs | |
export VENV_PYTHON=/usr/bin/python3.6 | |
fn_workon() { | |
if [ -f "${VENV_HOME}/${1}/bin/activate" ]; then | |
export VENV_CURRENT="${VENV_HOME}/${1}" | |
# Run commands before activation | |
if [ -f "${VENV_CURRENT}/pre_activate.sh" ]; then | |
. "${VENV_CURRENT}/pre_activate.sh" | |
fi | |
echo "Activating venv ${1}..." | |
. "${VENV_CURRENT}/bin/activate" | |
# Run commands after activation | |
if [ -f "${VENV_CURRENT}/post_activate.sh" ]; then | |
. "${VENV_CURRENT}/post_activate.sh" | |
fi | |
else | |
echo "Could not find the venv '${1}'. Here is a list of venvs:" | |
fn_lsvirtualenv | |
fi | |
} | |
alias workon=fn_workon | |
fn_mkvirtualenv() { | |
if [ -z "${VENV_HOME}" ]; then | |
echo "VENV_HOME is not set; we don't know where to create your venv." | |
else | |
echo "Creating a new venv at: ${VENV_HOME}/${1}..." | |
# Create the venv | |
${VENV_PYTHON} -m venv "${VENV_HOME}/${1}" | |
# Create script to run before venv activation | |
echo "# Commands to be run before venv activation" >> "${VENV_HOME}/${1}/pre_activate.sh" | |
# Create script to run after venv activation, default to current directory | |
echo "# Commands to be run after venv activation" >> "${VENV_HOME}/${1}/post_activate.sh" | |
echo "cd ${PWD}" >> "${VENV_HOME}/${1}/post_activate.sh" | |
# Activate the new venv | |
fn_workon "${1}" | |
# Get the latest pip | |
echo "Upgrading to latest pip..." | |
pip install --quiet --upgrade pip | |
fi | |
} | |
alias mkvirtualenv=fn_mkvirtualenv | |
fn_rmvirtualenv() { | |
if [ -z "${VENV_HOME}" ]; then | |
echo "VENV_HOME is not set; not removing." | |
else | |
echo "Removing venv at: ${VENV_HOME}/${1}..." | |
deactivate 2>/dev/null | |
unset VENV_CURRENT | |
rm -rf "${VENV_HOME}/${1}" | |
fi | |
} | |
alias rmvirtualenv=fn_rmvirtualenv | |
fn_lsvirtualenv() { | |
if [ -z "${VENV_HOME}" ]; then | |
echo "VENV_HOME is not set; can not show venvs." | |
else | |
ls -1 "${VENV_HOME}/" | |
fi | |
} | |
alias lsvirtualenv=fn_lsvirtualenv | |
fn_cdvirtualenv() { | |
cd "${VENV_CURRENT}" | |
} | |
alias cdvirtualenv=fn_cdvirtualenv | |
# Tab Completions | |
_venv_completions() | |
{ | |
COMPREPLY=($(compgen -W "$(ls $VENV_HOME)")) | |
} | |
complete -F _venv_completions workon | |
complete -F _venv_completions cdvirtualenv | |
complete -F _venv_completions rmvirtualenv |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I have a similar setup for PowerShell. Works well in both Windows PowerShell, and the new cross-platform open source PowerShell Core (which I really am loving).
Put this in your profile, which you can open with:
notepad $PROFILE
from a powershell prompt.Update 2023: I have improved my script a bit, so this comment was edited with the new scripts below. I use
--symlinks
when creating a virtualenv, so if the python version on the system is updated, it is updated automatically in all the venvs you created. Out of convention, I also put my venvs in the project folder in a folder named.venv
, so you'll want to gitignore that.