How to Install Python on Dreamhost Shared Hosting 15

Posted by ben on April 01, 2010

This article describes how to install and set up Python and VirtualEnv on Dreamhost web site hosting.

Some of these notes are based on this article: URL: http://blog.localkinegrinds.com/2007/08/20/custom-python-installation-for-django-on-dreamhost/

Set up

Log into your shared server and do the following:

$ mkdir -p opt/python261
$ mkdir downloads
$ cd downloads
$ wget http://www.python.org/ftp/python/2.6.1/Python-2.6.1.tgz
$ tar xvzf Python-2.6.1.tgz

Note that: According to the Filesystem Hierarchy Standard, the /opt dir “is reserved for the installation of add-on application software packages.”

Now configure where your new Python instance will be installed:

$ cd Python-2.6.1
$ ./configure --prefix=$HOME/opt/python261
$ make
$ make install > install.txt

NB: The install.txt file is useful to locate files if you wish to uninstall Python later. NB: you might wish to use:

$ ./configure --prefix=$HOME/opt/python261  --enable-unicode=ucs4

But Dreamhost does uses ucs2 by default (Python’s default to), and there can be issues if you try to load packages that are compiled using a different Unicode setting.

Now edit your ~/.bash_profile file to specify the new Python install located in: ~/opt/

export PATH=$PATH:$HOME/opt/python261/bin

You might also want to add an alias so that instead of typing python2.6 to access the python shell, you can just type python in the command line.

alias python='python2.6'

Don’t forget to tell bash to load this file after you have made your changes:

$ source ~/.bash_profile
$ python -V  # will display your new version 2.6.1

Installing VirtualEnv

$ cd downloads/
$ curl -O http://pypi.python.org/packages/source/v/virtualenv/virtualenv-1.4.5.tar.gz
$ tar -xvzf virtualenv-1.4.5.tar.gz
$ python virtualenv-1.4.5/virtualenv.py $HOME/virtual --no-site-packages

NB: This binds your new install with virtualenv and creates a virtual directory in your user home directory. You will need to use the –no-site-packages option so that virtualenv doesn’t use any of the original Dreamhost Python install packages.

Update your ~/.bash_profile file so that it puts the virtualenv packages onto the Python path, and activate(s) your virtual environment:

export PATH="$PATH:$HOME/opt/python261/bin:$HOME/virtual/bin"
source $HOME/virtual/bin/activate

Check that the packages directory is in your new virtual site_packages directory:

$ python -c "from distutils.sysconfig import get_python_lib; print get_python_lib()"

Now activate your virtual environment, by executing the .bash_profile file:

$ source ~/.bash_profile

Or manually activate it by:

$ . ~/virtual/bin activate

Installing MySQL Python Bindings

(virtual)$ cd downloads
(virtual)$ wget http://internap.dl.sourceforge.net/sourceforge/mysql-python/MySQL-python-1.2.3c1.tar.gz
(virtual)$ tar xvzf MySQL-python-1.2.3c1.tar.gz
(virtual)$ cd MySQL-python-1.2.3c1
(virtual)$ python setup.py install

Check that you can import the database correctly (NB: do this from a different directory from the one where MySQL-python was installed, as you will get Module already imported warnings):

(virtual)$ cd ~
(virtual)$ python
>>> import MySQLdb

If you don’t get an error all succeeded OK.

Install Django

(virtual)$ pip install Django

Everyday MySQL Commands

Posted by ben on August 18, 2008

I’m trying to put together a number of blog articles and many of them use the MySQL database, so I figured that I would post up some of my most used everyday MySQL SQL commands. Most likely I will append further commands as I write more articles so this is page is really a work in progress.

I’ve also assumed that you want to have full rights when executing these commands so I’ve used sudo, however you may not need this depending on your set up.

Finally if some other web site covers the command better or I’m feeling lazy I’ll just pop it in the links list at the bottom of this page.

Useful MySQL Commands

Starting MySQL Demon

Start your MySQL database and get it running as a background process:

sudo mysqld_safe
Ctrl + Z
bg

MySQL create databases script

Make a SQL script that creates 3 databases and assigns all rights to my username. Copy the following SQL and paste it into a file called create_databases.sql:

create database mydb_development;
create database mydb_test;
create database mydb_production;
grant all on mydb_development.* to 'my_username'@'localhost';
grant all on mydb_test.* to 'my_username'@'localhost';
grant all on mydb_production.* to 'my_username'@'localhost' identified by 'my_password';

MySQL drop databases script

Make a script to drop databases. Copy the following SQL and paste it into a file called drop_databases.sql:

drop database nemos_production;
drop database mydb_development;
drop database nemos_test;

Executing a script from MySQL

The above scripts can be executed from the command-line with the either of these commands:

sudo mysql < create_database.sql
or
sudo mysql < drop_database.sql

Showing MySQL databases

To check that the databases were created :

sudo mysqlshow
or
sudo mysql
show databases;

Displaying database tables

Displaying the tables of a database:

sudo mysqlshow mydb_development
or
sudo mysql
show tables from mydb_development;

Displaying details about a database table

Displaying details about a particular database table:

sudo mysqlshow mydb_development mytable

Droping a database table

Dropping a table from a database:

sudo mysql
use mydb_development;
drop table mytable;

Displaying information about a table

Displaying the fields and field metadata about a table:

show fields from my_table; 
or
desc my_table;

Useful Links

These are just a few quick and simple MySQL commands that you will always use. Hope this helps.