venv - a very simple python virtual environment manager

Though my primary focus is in Rust, I've had quite a few projects in Python recently. Over the past year, I've gained more and more appreciation for virtual environments. They're very useful for both development and deployment, in situations where you don't want to clutter the host system with a bunch of random modules pertaining to one program. However, there are a few things that just don't sit well with me.

  • Activating them is a PITA. Firstly, you have to remember (or look for) the directory that you left your virtualenv in for the project. Typically, the searching isn't the hard part, but having to write out the entire path to the activate source file just isn't a pretty solution here.
  • Speaking of the directory, what is the freaking standard? Am I going to put it in ./.venv, ./venv, ~/.venv/..., ~/venvs/...?? Nobody seems to have the same answer to this question, and that kind of uncertainty in my life upsets me.
  • Deactivating - is there even such a thing? Yes, I know that there really isn't a point in "deactivating" the environment because there's not much harm, but sometimes I just want to reuse the shell in-place without reopening it (tiling WMs for the win). Additionally, that tag on the prompt is a major distraction for me - it does not belong there if that's not what I'm using this shell for anymore.
  • Oh wait, that tag. Isn't it kind of annoying that it looks like (.venv) with no descriptive name? And if I want to create a custom name, I have to use the --prompt flag on creation? Why would this kind of functionality be imnplemented in the most inconvenient/non-ergonomic way?

This last month or so, I sat down and wrote a few scripts and set a few standards for myself for managing my virtual environments. This is the end result; I've compiled them into a single shell script that I can distribute to wherever I need it. It features a central directory for storing all created virtualenvs, and it sources the activate script in a new shell process so I can type exit and be back at the default prompt.

Source Code

This work is licensed as public domain; I have no intent to try to protect it as my "intellectual property." Do with it what you wish, and I hope you find it useful!

The source is alsohosted on GitLab.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#!/bin/sh
#
# virtual environment management script
# author: adam gausmann
# licensed under public domain. you are free to use, modify,
#   and distribute as you wish.
#

VENV_HOME="$HOME/.venv"

case $1 in
create)
    if [ -e "$VENV_HOME/$2" ]
    then
        echo "'$2' already exists. To activate, type \`$0 activate $2\`."
        exit 1
    fi

    mkdir "$VENV_HOME/$2"
    virtualenv "$VENV_HOME/$2"
    echo "Virtual environment '$2' created. To activate, type \`$0 activate $2\`."
;;

delete)
    if ! [ -e "$VENV_HOME/$2" ]
    then
        echo "Virtual environment '$2' does not exist."
        exit 1
    fi

    rm -r "$VENV_HOME/$2"
    echo "Virtual environment '$2' deleted."
;;

activate)
    if ! [ -e "$VENV_HOME/$2" ]
    then
        echo "Virtual environment '$2' does not exist."
        exit 1
    fi

    echo "'$2' activated. To deactivate, type \`exit\`."
    bash --init-file "$VENV_HOME/$2/bin/activate"
;;
esac

links

social