Extending Python virtualenv using Bootstrap Scripts

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:

  1. creating a virtual environment my_app
  2. installing the modules boto and mysql-connector-python
  3. running my_app.py after modules have been installed

Possible Solutions:

  1. Using a shell script to create and then customize the virtual environment. For the impatient: my_app_bootstrap.sh
  2. 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:

  1. creates a virtual environment named my_app
  2. activates the my_app virtual environment
  3. installs additional modules in my_app using pip
  4. runs my_app.py

virtualenv Bootstrapping:

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:

  • extend_parser
  • adjust_option
  • after_install

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:

  1. 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:

  1. provide the ability to create and update the after_install script and then inject it into the bootstrap file
  2. 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:

  1. read an after_install script – by default this is my_app_after_install.py
  2. insert this into the default virtualenv bootstrap script as an after_install function
  3. write out the resulting bootstrap file as my_app_bootstrap.py

create_my_app_bootstrap.py’s function is described in the diagram below:


In Summary:

  • 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.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s