Python and Virtualenv

1and1 doesn't give us a modern version of Python, so it is necessary to use virtualenv - a Python sandboxing script. .

virtualenv replaces the system Python (and assicated libraries and install packages) with locally-installed versions for as long as the environment is active. (Read about virtualenv here)

Activating the Python 2.7.2 Virtualenv on

There is a Python 2.7.2 environment already set up for you. You can access it by:

  1. Logging in to over ssh
  2. Typing source ~/newpy/bin/activate at the commandline

Deactivating the Python 2.7.2 Virtualenv on

To deactivate the same environment, just type deactivate (doesn't matter what dir you're in).

Installing packages from withing the virtualenv

Should you need Python packages that are not available from the system Python, you can always install them in the virtualenv. If you know the package name, from the commandline type:

pip install {packagename}

Obviously, this uses an installer program called pip, which comes prepackaged with virtualenv. You can read more about pip here.

Listing all the installed packages in a virtual environment

If you are curious what has already been installed in the current virtual environment, pip can tell you.

pip freeze

Creating a new virtual environment

If you need a different version of Python from the one installed in the newpy environment, you cannot simply install a new one with pip. Rather, you'll have to download the source, compile it yourself, and then create a new virtual environemnt based off of the new version. The instructions for this are taken from this StackOverflow post.

NOTE: the ~/.localpython directory suggested in the post is already set up for you)

  1. wget{version}/Python-{version}.tgz (NOTE: you'll have to find the path yourself - at the time of writing, that's where Python source lives)
  2. tar xzf Python-{version}.tgz
  3. cd Python-{version}
  4. ./configure --prefix=/kunden/homepages/43/d109612362/htdocs/.localpython
  5. make
  6. make install
  7. cd
  8. source newpy/bin/activate
  9. virtualenv {name-of-new-environment} -p /kunden/homepages/43/d109612362/htdocs/.localpython/bin/python{version}

That creates a new virtual environment with a name of your choosing against the new Python version. Activate this environment whenever you want to run programs against that version.