23 03
2009

Helper script for virtual Python setups

This is deprecated, use virtualenv instead.

Sometimes (too often, actually) I find myself setting up yet another collection of libraries for some specific Python project. For this I've half-baked a solution. The following script does the dirty-work. You just use the Python binary from install-dir/bin.

#!/bin/sh

# vpython-setup.sh
# Helper script for setting up virtual Python instances, usage instructions included

# (c) 2009 Atte Hinkka (first.last@iki.fi)

# This work ‘as-is’ we provide.
# No warranty, express or implied.
# We’ve done our best,
# to debug and test.
# Liability for damages denied.
#
# Permission is granted hereby,
# to copy, share, and modify.
# Use as is fit,
# free or for profit.
# On this notice these rights rely.


if [ -d "${1}" ]; then
    DEST=$1
else
    echo "Please create the Python env target dir and pass it as the first argument."
    exit 0
fi

ORIG_PATH=`pwd`
cd ${1}
TARGET_PATH=`pwd`
cd $ORIG_PATH

export PYTHONPATH=${TARGET_PATH}:$PYTHONPATH
wget http://peak.telecommunity.com/dist/virtual-python.py
python virtual-python.py --prefix=${TARGET_PATH}
rm virtual-python.py
wget -O ${TARGET_PATH}/ez_setup.py http://peak.telecommunity.com/dist/ez_setup.py

touch ${TARGET_PATH}/bin/easy_install
echo "#!/bin/sh">>${TARGET_PATH}/bin/easy_install
echo "">>${TARGET_PATH}/bin/easy_install
echo "${TARGET_PATH}/bin/python ${TARGET_PATH}/ez_setup.py">>${TARGET_PATH}/bin/easy_install
chmod +x ${TARGET_PATH}/bin/easy_install

cd $TARGET_PATH
${TARGET_PATH}/bin/python ez_setup.py setuptools
cd $ORIG_PATH

cat >${TARGET_PATH}/init-python-env.sh <<\EOF
# Source this file to use this Python environment
# Additional libraries can be installed with ez_setup.py.
# Some libs need --prefix, use PYTHONPATH for that.
#  I.e. python ez_setup.py SQLAlchemy==0.4.8
#  or cd Django-1.0 && python setup.py install --prefix=$PYTHONPATH
#
# To use this environment in a script (or web app) add
EOF
echo "#   #!${TARGET_PATH}/bin/python">>${TARGET_PATH}/init-python-env.sh
echo "# to the first line of the script.">>${TARGET_PATH}/init-python-env.sh
echo "# Sometimes it's also helpful to append ">>${TARGET_PATH}/init-python-env.sh
echo "#  ${TARGET_PATH}/lib/python2.4/site-packages to PYTHONPATH.">>${TARGET_PATH}/init-python-env.sh
echo "">>${TARGET_PATH}/init-python-env.sh
echo "export PYTHONPATH=${TARGET_PATH}">>${TARGET_PATH}/init-python-env.sh
echo "export PATH=${TARGET_PATH}/bin:\$PATH">>${TARGET_PATH}/init-python-env.sh

echo ""
echo "See ${TARGET_PATH}/init-python-env.sh for additional instructions."