Painless Django with MySQL Install

When using Django for developing a web applications, it’s common to use MySQL as a data repository.
In order to use this combination of technolgies, one needs to setup all the different pieces. Setting up all this may not be as easy as it may seem and it actually will take a bit longer than one may expect.
To ease this process, here’s a simple step-by-step guide to help seting a Django with MySQL development environment.

The first step is to download and install Python. Current Django version, 1.3, does not work on Python 3, so download the latest Python 2 version, presently 2.7.2, or use your system package manager to get it from the official repository.

The second step is to download and install MySQL.
Check your system package manager to get it from the official repository, if available.
Don’t try to use Xampp. It may work but you’ll have to hack some installation procedures, specially in Windows 64 bit. For instance, the database connector will look up  the MySQL location in the Windows registry and it will be missing.
I recommend the MySQL bundle, since it will come with useful software for database management.

The third step is to download and install Python setuptools.
If you’re using Windows 64 bits, and you’re getting a “Python not found on Registry” error, you may use this workaround.

Finally, we need to download the MySQL-Python connector so that we can use MySQL from Python.
Uncompress the file and, on a shell, perform the standard installation procedure: python setup.py install. Depending on your system, you may need to run this with administration privilidges.
If you’re using Windows, I seriously recommend that you download and install codegood build. If you don’t wish to use codegood build and are getting file not found errors originated from a registry key not found – usually in 64 bit versions – just edit the site.cfg file with a plain text editor and change the registry_key to “SOFTWARE\Wow6432Node\MySQL AB\MySQL Server 5.5“, in 64 bit, taking in consideration that the version numbers should correspond to your MySQL installed version. When in doubt, use Windows Registry application, regedit.exe, to check the correct registry key.

Optionally, if you’re installing on Windows, I recommend you to install MSYS and to run the Django commands from this shell.
This will enable you a linux like shell that is helpful to process some commands like localization, e. g. the gettext tool. If you don’t wish to install MSYS, check the Django documentation on how to get the required tools to work on Windows.

Since all the technologies are always under development, make sure you get the right versions of each so that things work properly.

After following these steps, Python should connect to MySQL, and so should Django.

R Package with S4 Objects

When performing R packages, Linux may be the best choice because R packaging in Windows implies to install a lot of base Linux applications and tools on Windows. For those that wish to go that road, here’s what one has to do: read Making tutorial about R Packages Under Windows: A Tutorial by Peter Rossi.
For everyone else, just boot your Linux.

Before starting, one should take a look at Writting R Extensions. This is the first step for all that wish to build R extensions. A fast and easy way to know how to pack, is to read An Introduction to the R package mechanism.

When developing using S4 objects, packaging may become a big headache.
I’ve suffered this headache. I had all kinds of warnings and errors during the package tests and installation.

I’ve started by posting to the R-Help list asking for help. Here’s the post transcript:

Here's what I do:

1. in R console, I do and get:
> package.skeleton(name='remora')remora-package
Creating directories ...
Creating DESCRIPTION ...
Creating Read-and-delete-me ...
Saving functions and data ...
Making help files ...
Done.
Further steps are described in './remora/Read-and-delete-me'.
Warning messages:
1: In dump(internalObjs, file = file.path(code_dir,
sprintf("%s-internal.R",  :
deparse of an S4 object will not be
source()able
2: In dump(internalObjs, file = file.path(code_dir,
sprintf("%s-internal.R",  :
deparse of an S4 object will not be
source()able
3: In dump(internalObjs, file = file.path(code_dir,
sprintf("%s-internal.R",  :
deparse of an S4 object will not be
source()able
4: In dump(internalObjs, file = file.path(code_dir,
sprintf("%s-internal.R",  :
deparse of an S4 object will not be source()able

I don't know why I get these warnings. 
I've followed R implementation rules and the S4 objects work fine.
2. Performing the 'R CMD build remora' command I get:

* checking for file 'remora/DESCRIPTION' ... OK
* preparing 'remora':
* checking DESCRIPTION meta-information ... OK
* removing junk files
* checking for LF line-endings in source and make files
* checking for empty or unneeded directories
* building 'remora_1.0.tar.gz'

And the remora_1.0.tar.gz file seems ok.
3. Performing the 'R CMD check remora' command I get:

* checking for working pdflatex ...sh: pdflatex: not found
NO
* checking for working latex ...sh: latex: not found
NO
* using log directory '/home/fmm/thesis/R/src/remora.Rcheck'
* using R version 2.8.1 (2008-12-22)
* using session charset: UTF-8
* checking for file 'remora/DESCRIPTION' ... OK
* checking extension type ... Package
* this is package 'remora' version '1.0'
* checking package dependencies ... OK
* checking if this is a source package ... OK
* checking for executable files ... OK
* checking whether package 'remora' can be installed ...
ERROR
Installation failed.
See '/home/fmm/thesis/R/src/remora.Rcheck/00install.out'
for details.
4. the log file contains:

* Installing *source* package 'remora'
...
**
R

**
data

** preparing package for lazy
loading
Error in parse(n = -1, file = file) : unexpected '-> code2LazyLoadDB
-> sys.source -> parse Execution halted
ERROR: lazy loading failed for package
'remora'
** Removing
'/home/fmm/thesis/R/src/remora.Rcheck/remora'
fmm@Darkmaster:~/thesis/R/src$ grep -i __C__remoraConfiguration *
fmm@Darkmaster:~/thesis/R/src$ grep -i __C__remoraConfiguration */*
remora.Rcheck/00install.out:745: `.__C__remoraConfiguration`

But, unfortunately, no help came from there…

In the quest for the solution I’ve found out that many others were having similar problems with S4 object packaging, but no solutions were provided.
After a lot investigation, I’ve finally found it and the solution is actually quite easy.
Instead of packing it from the current environment, I’ve just passed to the package.skeleton the list of source files to build the package. As an example, here’s the small R script I’ve done to automate the packaging procedure for my “Remora” package:

cat('\nPacking Remora...\n')

file_lst <- character(5)
file_lst[1] <- '/home/m6/thesis/R/src/1_classes.r'
file_lst[2] <- '/home/m6/thesis/R/src/2_common.r'
file_lst[3] <- '/home/m6/thesis/R/src/3_model.r'
file_lst[4] <- '/home/m6/thesis/R/src/4_predict.r'
file_lst[5] <- '/home/m6/thesis/R/src/5_main.r'   

package.skeleton(name = "remora", force = TRUE, namespace = TRUE,
code_files = file_lst)

cat('\nDone.\n')

Now it's time to go to the directory created, with the name of the package,from now on {package}, and edit the following files:

  • {package}/DESCRIPTION, the description of the package;
  • {package}/NAMESPACE, the list of functions and classes to export to the user;
  • {package}/man/{package}-package.Rd, the package help file, the \examples section must provide executable code, since R check command will execute this code;
  • {package}/man/{class_name}-class.Rd, the classes help files;
  • {package}/man/{function_name}.Rd, the functions help files;

All files under /man/ are tex files and will be compiled to provide the functions help when invoked by the user.
It's only necessary to document the classes and functions that will be exported, i. e. exported in the NAMESPACE file, since all the others will not be visible to the user. All the other .Rd files may be deleted.

After the package has been created, I've tested with the "R CMD check {package}" command. My package name is "remora", so my command was "sudo R CMD check remora".
I had to run this command with the administration role, so I prefixed it with the"sudo" command. This is a characteristic my Kubuntu installation and one may not require to perform this with administrator privilegies.

Finally I've build the package with the "R CMD build {package}" command that created the tar.gz file for distribution.

To install it, just use the "R CMD INSTALL {package}" command. I've entered R and it worked fine.
To uninstall just execute "R CMD REMOVE {package}".