Extending Python virtualenv with Bootstrap Scripts
extending a python virtual environment immediately after the virtual environment has been created. The virtualenv documentation refers to this as bootstrapping. For those who are hands-on, I’ve included a link to a zip file containing the scripts described in this post.
Most typically “bootstrapping” would be used when describing running a Python application within a virtual environment. As an example this might entail:
- creating a virtual environment my_app
- installing the modules boto and mysql-connector-python
- running my_app.py after modules have been installed
- Using a shell script to create and then customize the virtual environment. For the impatient: my_app_bootstrap.sh
- Using a virtualenv “Bootstrap Scripts” that runs “after_install” once an environment has been created. For the impatient: my_app_bootstrap.py my_app
Shell Script Bootstrapping:
To test the shell script method of bootstrapping I created a simple shell script that does the following:
- creates a virtual environment named my_app
- activates the my_app virtual environment
- installs additional modules in my_app using pip
- runs my_app.py
The authors of virtualenv have provided a means of bootstrapping virtual environments through the creation custom virtualenv “bootstrap scripts” and the injection of three functions into these scripts. The three functions are listed below:
Customizing a virtual environment required only that I utilize the “after_install” function – after_install met my need of installing modules after the virtual environment has been created. I wanted to experiment with bootstrap scripts and after_install further, so I devised two tests.
Test 1: Creation of a Default Bootstrap Script (no after_install)
Test one was to simply create a default virtualenv bootstrap script using the virtualenv function “create_bootstrap_script.” I did this utilizing a script called create_default_bootstrap.py. The resulting bootstrap script (named basic_bootstrap_script.py) is nearly the same as virtualenv.py. I diff’ed both basic_bootstrap_script.py and virtualenv.py – there are no meaningful differences.
Test 2: Creation of a Custom Bootstrap Script (using after_install)
Test two had one objective:
- customize the newly created my_app virtual environment by adding python modules after install.
Having worked with virtualenv bootstrap scripts before and been frustrated with “including a script in a script”, I added additional objectives:
- provide the ability to create and update the after_install script and then inject it into the bootstrap file
- provide output to standard out notifying the user that after_install had been called
My solution was to create a script (named: create_my_app_bootstrap.py) that did the following:
- read an after_install script – by default this is my_app_after_install.py
- insert this into the default virtualenv bootstrap script as an after_install function
- write out the resulting bootstrap file as my_app_bootstrap.py
create_my_app_bootstrap.py’s function is described in the diagram below:
- Either shell script or after_install method of bootstrapping worked equally well.
- The shell script method is easier to learn and troubleshoot.
- Those implementing virtualenv functionality may wish to look at http://docs.python.org/3/library/venv.html.