Archive for the 'Python' Category

Installing Django with PostgreSQL on Ubuntu

Antonio Cangiano December 26th, 2007

This how-to is essentially the same as my previous one, only this time I’ve provided step-by-step instructions for installing Django with PostgreSQL on Ubuntu 7.10.

First and foremost, we are going to install Django from its svn repository, as opposed to obtaining the 0.96 release archive. The reason for this is that the trunk version implements a few new features. The development code is also rather stable and used by most people in production mode, even for sites like the Washington Post.

Install Subversion

sudo apt-get install subversion

Checkout Django

svn co http://code.djangoproject.com/svn/django/trunk django_trunk

Tell Python where Django is

Ubuntu already ships with Python 2.5.1, thus you won’t have to install it. You can verify this by running python in your shell (use exit() to get out of the python shell). What you need to do is inform Python about the location of your django_trunk directory. To do this create the following file:

/usr/lib/python2.5/site-packages/django.pth

Within this file, place only one line containing the path to your django_trunk folder. In my case, this is:

/home/antonio/django_trunk

Of course, change it to the full path location of the directory on your filesystem.

Add django-admin.py to your PATH

The bin directory within the django folder (which is inside django_trunk itself) contains several management utilities. We need therefore to add the following to the PATH (again, change it to your own location):

/home/antonio/django_trunk/django/bin

How you go about doing this, depends on the shell you are using, and I’m assuming you are able to export a shell variable on your own. In case you are using the bash shell (as I do) you could export it in .bashrc. Alternatively, you could just create a symlink to the utility django-admin.py in /usr/bin, but I recommend the former approach.

Install PostgreSQL and Psycopg2

sudo apt-get install postgresql pgadmin3 python-psycopg2

This will install PostgreSQL 8.2.5, PgAdmin III and the driver Psycopg2 for you. Most people at this point will ask, what’s the default password for PostgreSQL on Ubuntu? You can use the following instructions to set the password for the user postgres both in Ubuntu and within PostgreSQL:

sudo su -
passwd postgres
su postgres
psql template1

The last instruction should open the psql shell, where you can run the following:

ALTER USER postgres WITH ENCRYPTED PASSWORD ‘mypassword’;

Verify the installation

You should be all set now, but let’s verify this right away. Open the shell and run the following instructions inside the python shell (start off with the python command).

>>> import django
>>> print django.VERSION
(0, 97, ‘pre’)
>>> import psycopg2
>>> psycopg2.apilevel
‘2.0′

By running exit() get out of the python shell, and verify that django-admin.py is in your path:

django-admin.py
Type ‘django-admin.py help’ for usage.

If you obtain a similar output for all three of them, you are really set.

Where to go from here

Now that Django is installed, you can go read the Django Book 1.0 that’s available for free online. Something equally well done and useful is really missing from the Rails community. Above all, experiment, Django (and programming in general) is learnt by doing. The Definitive Guide to Django: Web Development Done Right is also available for purchase in its deadtree version, which just came out. It’s cheap and it’s already a best seller on Amazon. Despite the availably of a free version online, I like having paper versions of tech books so that I can read without staring at the monitor. Furthermore, I feel like rewarding the authors (who are also the framework creators), while encouraging publishing companies that are willing to allow authors to make their books available for free on the web. Well done guys!

How to install Django with MySQL on Mac OS X

Antonio Cangiano December 22nd, 2007

Installing Django on Mac OS X Leopard is supposed to be very straightforward, but if you are new to it, you may encounter a few puzzling questions and, in the case of MySQL, even a couple of headaches. I’m writing about this for the benefit of those of you who may attempt and struggle with this feat. MacPorts is not required for this how-to.

First and foremost, we are going to install Django from its svn repository, as opposed to obtaining the 0.96 release archive. The reason for this is that the trunk version implements a few new features. The development code is also rather stable and used by most people in production mode, even for sites like the Washington Post.

Checkout Django

svn co http://code.djangoproject.com/svn/django/trunk django_trunk

Tell Python where Django is

Mac OS X 10.5 already ships with Python 2.5.1, thus you won’t have to install it. You can verify this by running python in the Terminal (use exit() to get out of the python shell). What you need to do is inform Python about the location of your django_trunk directory. To do this create the following file:

/Library/Python/2.5/site-packages/django.pth

Within this file, place only one line containing the path to your django_trunk folder. In my case, this is:

/Users/Antonio/Code/django_trunk

Of course, change it to the full path location of the directory on your filesystem.

Add django-admin.py to your PATH

The bin directory within the django folder (which is inside django_trunk itself) contains several management utilities. We need therefore to add the following to the PATH (again, change it to your own location):

/Users/Antonio/Code/django_trunk/django/bin

How you go about doing this, depends on the shell you are using, and I’m assuming you are able to export a shell variable on your own. In case you are using the bash shell (as I do) then you should have a .profile file in your home directory. Alternatively, you could just create a symlink to the utility django-admin.py in /usr/bin, but I recommend the former approach.

Grab and install MySQL

I would normally recommend PostgreSQL, at least until we have DB2 on Mac, but I realize that many of you use and prefer MySQL, which also seems to be the only one that requires special instructions due to a few installation issues when trying to get MySQL and Python to work together. You can install MySQL by grabbing and running one of the packages that are available on the official site. Choose the one for x86 and Mac OS X 10.4.

Install the MySQLdb driver

Get MySQL-python-1.2.2.tar.gz from SourceForge. Please follow these exact instructions because the source code won’t compile out of the box and will give you the following error when trying to build it:

/usr/include/sys/types.h:92: error: duplicate ‘unsigned’
/usr/include/sys/types.h:92: error: two or more data types
in declaration specifiers
error: Setup script exited with error: command ‘gcc’ failed

Run the following:

tar xvfz MySQL-python-1.2.2.tar.gz
cd MySQL-python-1.2.2

At this point, edit the _mysql.c file and comment out lines 37, 38 and 39 as follows:

//#ifndef uint
//#define uint unsigned int
//#endif

Now, from the MySQL-python-1.2.2 folder run:

python setup.py build
sudo python setup.py install

If you still get an error (and only in that case) you’ll need to edit the site.cfg file within the same folder and set threadsafe = False, before running the two commands above once again.
If instead, you don’t receive an error but you see warnings about files not required on this architecture, don’t be concerned about them. The last step required is to create a symbolic link with the following command:

sudo ln -s /usr/local/mysql/lib/ /usr/local/mysql/lib/mysql

All these adjustments are required because we are building and installing the driver on Mac and not on Linux.

Verify the installation

You should be all set now, but let’s verify this right away. Open Terminal and run the following commands in the python shell (start this with the python command).

Verify that MySQLdb is correctly installed:

>>> import MySQLdb
>>> MySQLdb.apilevel
‘2.0′

Now, verify that Django is working:

>>> import django
>>> print django.VERSION
(0, 97, ‘pre’)

By running exit() get out of the python shell, and verify that django-admin.py is in your path:

django-admin.py
Type ‘django-admin.py help’ for usage.

If you obtain a similar output for all three of them, you are really set to write the next YouTube.

Where to go from here

Now that Django is installed, you can go read the Django Book 1.0 that’s available for free online. Something equally well done and useful is really missing from the Rails community. Above all, experiment, Django (and programming in general) is learnt by doing. The Definitive Guide to Django: Web Development Done Right is also available for purchase in its deadtree version, which just came out. It’s cheap and it’s already a best seller on Amazon. Despite the availably of a free version online, I like having paper versions of tech books so that I can read without staring at the monitor. Furthermore, I feel like rewarding the authors (who are also the framework creators), while encouraging publishing companies that are willing to allow authors to make their books available for free on the web. Well done guys!

Programmers Ad Community: 50 invites for Adroll’s private beta

Antonio Cangiano December 21st, 2007

For a month or so I’ve been running (or should I say rolling?) my ads with Adroll.com. I want to briefly introduce this cool niche network ad service to other programmers, so that we stop receiving travel ads whenever we talk about Rails.

Traditionally website and blog owners put Google Ads up on their sites. In most cases, unless you are uber-popular, Google gives you peanuts. This is particularly true for technical sites where the audience is very web-savvy and rarely clicks on ads. On top of that, and rightfully so, many programmers use Adblock Plus and won’t see your ads either, unlike the viewing public of more general sites. Granted ads are there mostly to recoup expenses for the server and they aren’t expected to turn a profit in my case, obtaining a decent eCPM with a programming blog is not an easy task for anyone. There is a big earning difference between the obtainable eCPM when running Google Ads or when serving ads for a Sponsor’s campaign. Sponsors’ campaigns, whether click or impression based, can give you a lot of cash to buy all those cool books and even a nice treat. The problem is that unless your site is popular, you won’t be of any interest to advertisement agencies who work on behalf of big companies. They are looking for millions of impressions and chances are that your programming blog won’t ever reach anything remotely close to that. So you are stuck with ridiculously low eCPMs from Google, which serves thousands upon thousands of impressions for less than a breakfast at Starbucks.

This is where Adroll kicks in. Adroll isn’t too well known yet, but they are growing steadily and are “revolutionary” in their own way. Adroll allows advertisers to buy ad campaigns on your website. Nothing new there, I can hear you say. The innovation relies on the fact that with Adroll you can create or join topical communities of sites. When you start to have a group of, say, a few dozen car sites and blogs, the number of pageviews and unique visitors becomes sufficiently large enough to interest the agencies who work on behalf of BMW or Mercedes, for example. And that’s when the real dough arrives. Sure, your site won’t be making a huge deal of money if it’s small, but the earning per thousand clicks or impressions is going to be far greater than what Google usually offers. And more importantly, ads will be relevant to the topic of your site and more likely to interest your visitors. With Adroll, you can also set the minimum amount of eCPM that you require from advertisers, therefore excluding campaigns that pay too little.

Not only that, but you can let your site participate in different topical communities, therefore increasing your chances of being part of a campaign from a good advertiser. If all this wasn’t neat enough, their service is simply perfect for managing your existing Google ads. You can even use it to rotate your typical ad services (Google, Yahoo, etc…) or your in-house ads, and Adroll won’t make a cent off you. You will still get paid 100% from Google and these other services, while Adroll just serves their ads and doesn’t enter into the monetary picture at all. For example, right now, as I wait for a few of you to join me, I’ve been using it to serve Google ads in two spots, and “advertise here” in a third spot. Adroll provides you with detailed and up to date statistics and also shows advertisers the most common keywords used to reach your site.

Here is an illustration and their own description (from the about) of their services:

Adroll Structure

“Problems we solve for advertisers:

  • It’s too hard to find and access all the small sites that are well targeted for my products.
  • It’s prohibitively time consuming to buy and manage campaigns across these targeted sites.

Problems we solve for online publishers:

  • Advertisers don’t know about me because I’m a smaller niche site.
  • Selling to direct advertisers is manual and requires negotiating individual contracts, and switching ads by hand.”

Long story short, I was able to obtain 50 invites for their private beta. These are going fast, so grab one for your programming blog or site now. I also created a Programmers Ad Community, which I invite all interested programming and development bloggers (and site owners) to join.

You simply need to do the following:

  1. Register and use ZENOFPROGRAMMING as your promocode to be accepted to the private beta.
  2. Once your site(s) is(are) all setup within Adroll, add it(them) to the community: Programmers Ad Community.

For those who are curious, this San Francisco company is powered by Python and proudly uses Twisted Matrix. Their team has hardcore hackers like my friend Valentino Volonghi and SQLAlchemy’s creator Mike Bayer. Those are just two of the smart cookies who have been hired by this company. And in fact, Adroll works very well, which is rare for a beta product. I strongly believe that they will succeed in their intent of changing the ad market by providing relevant ads to niche content providers.

Zooppa

Since I’m on the unusual (for this site) topic of ads, I’d like to bring another cool startup called Zooppa, to your attention. This site is innovative and very popular in Italy and Europe, but it has been completely ignored by US tech bloggers, perhaps because it doesn’t burn millions of VC dollars and it’s not based in Silicon Valley or this side of the Atlantic. Techcrunch should really talk about this one.

Regardless, take a look at this site and you’ll be hooked. It works in the following way: an advertiser asks the community to create a video, print or radio ad for their product. The creative process starts and the participants’ work is voted on. The winner(s) gets the money and gets to see their work used by big companies like HTC, Citroen, Fineco, et cetera. This is good for big companies because each video, including those that don’t win, give the product a lot of exposure. Each clip boosts the brand’s image. It’s also good for creative people, who get rewarded with hard cash for their work, and non-professional filmmakers have for the first time a shot at working for a big company. For us spectators, the site is quite entertaining and there are plenty of funny clips to watch. I wonder how long it’ll take before YouTube adopts something similar?

« Prev - Next »