Category Archives: Solutions

Django : Python : Models Base.py IndexError: list index out of range

This page has the solution to IndexError Django throws when you try to access the model API from a standalone script.

I tried to import the model class after setting the PYTHONPATH and DJANGO_SETTINGS_MODULE Environment Variables

>>> from models import Player

This is the error I got,

.....
  File "C:\Python27\lib\site-packages\django\db\models\base.py", line 52, in __new__
    kwargs = {"app_label": model_module.__name__.split('.')[-2]}
IndexError: list index out of range

After some googling and code search, the base.py expects the app name and its not available when you try to access the model API from a standalone script.

So you have to add the meta information APP name to all the model classes under the models.py. That will get rid of this error.

class xxx(models.Model):
    name = models.CharField(max_length=200)
    country = models.CharField(max_length=200)
    twitter_handle = models.CharField(max_length=100)
    followers = models.IntegerField('number of twitter followers')
    
    def __unicode__(self):
        return self.twitter_handle
    class Meta:
        app_label = 'appname'
    
class yyy(models.Model):
    xxx= models.ForeignKey(xxx)
    conversation = models.CharField(max_length=10000)
    teaser = models.CharField(max_length=50)
    views = models.IntegerField('popularity')
    likes = models.IntegerField()
    published_date = models.DateTimeField('date_published')
    
    def __unicode__(self):
        return self.teaser
    class Meta:
        app_label = 'appname'

ImportError: Could not import settings xxx.settings : Django : Python : Windows

Recently, I built an application using Django. I wanted to create a standalone script that used the My App’s model API to insert data into Database.

The Django Document says:

“If you’d rather not use manage.py, no problem. Just make sure mysiteandpollsare at the root level on the Python path (i.e., import mysiteandimport pollswork) and set theDJANGO_SETTINGS_MODULE environment variable tomysite.settings.”

I was little confused about those statements.

So what is PYTHONPATH? In general, it’s the collection of all searchable paths where python would search for modules that you might use in your programs.

Initially, I screwed up the environmental variables and this is the error that I got.

 

 

......self._wrapped = Settings(settings_module)
File "C:\Python27\lib\site-packages\django\conf\__init__.py", line 89, in __init__
raise ImportError("Could not import settings '%s' (Is it on sys.path?): %s" % (self.SETTINGS_MODULE, e))
ImportError: Could not import settings 'LearnPython.settings' (Is it on sys.path?): No module named LearnPython.settings

So this is what I did:
1) Set the PYTHONPATH environment variable to point to the directory of your django app.

PYTHONPATH = “C:\path\to\myapp\”

this is the path under which you’ll have the models.py

2) set the DJANGO_SETTINGS_MODULE environmental variable. This is the settings module that your app will use to determine the database user,password , host etc.

In my case I set DJANGO_SETTINGS_MODULE = “myapp.settings”

To confirm that everything worked, after setting the environmental variables , I logged into a new command prompt ,

C:\Users\xxx>python
Python 2.7.2 (default, Jun 12 2011, 14:24:46).....
32
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> print os.environ['DJANGO_SETTINGS_MODULE']
cricketweet.settings
>>> from models import Player
>>>

So Now I can access the model classes from a stand alone app. Happy coding :)

Improperly Configured MySQL DB Module-Exception – Django – Windows 7

I had this error after I installed Django, created a project, modified the settings.py to use the MySQL database and while I tried to create the necessary tables in the MySQL by running the following command.

 python manage.py syncdb 

I got this exception

 .......

backend = load_backend(db['ENGINE'])
  File "c:\Python27\lib\site-packages\django\db\utils.py", line 33, in load_backend
    return import_module('.base', backend_name)
  File "c:\Python27\lib\site-packages\django\utils\importlib.py", line 35, in import_module
    __import__(name)
  File "c:\Python27\lib\site-packages\django\db\backends\mysql\base.py", line 14, in 
    raise ImproperlyConfigured("Error loading MySQLdb module: %s" % e)
django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: No module named MySQLdb 

I was using Python 2.7 , so the solution was to use the MySQL Python connector. I downloaded it from here

After , I Installed the MySQL Python Connector, I created the MySQL database,(make sure you commit it) This seemed to work fine.

 $ python manage.py syncdb
Creating tables ...
Creating table auth_permission
Creating table auth_group_permissions
Creating table auth_group
Creating table auth_user_user_permissions
Creating table auth_user_groups
Creating table auth_user
Creating table auth_message
Creating table django_content_type
Creating table django_session
Creating table django_site

You just installed Django's auth system, which means you don't have any superusers defined.
Would you like to create one now? (yes/no): yes
Username (Leave blank to use 'test'):
E-mail address: test@gmail.com
Password:
Password (again):
Superuser created successfully.
Installing custom SQL ...
Installing indexes ...
No fixtures found. 

Hopefully this helps.

sqlplus: error while loading shared libraries: libsqlplus.so wrong ELF class

sqlplus: error while loading shared libraries: libsqlplus.so wrong ELF class 

This error happens If you are working on a 64 bit operating system and pointing to a 32 bit library.

So this is how your environment variables should look like.

PATH=$PATH:$HOME/bin
ORACLE_HOME=/x/home/oracle/product/11.2.0.1
PATH=$ORACLE_HOME/bin:$PATH
PATH=$ORACLE_HOME/lib64:$PATH
LD_LIBRARY_PATH=$ORACLE_HOME/lib64
export ORACLE_HOME
export LD_LIBRARY_PATH
export PATH 

This will fix the issue.

SQLFeatureNotSupportedException – SOAP UI – Solution

Hi All,

I have been working on functional test automation for the API’s at work. We are using SoapUI for the automation of the functional tests. We wanted to invoke the test suite from the Junit test. When  I was trying to call the test case from JUnit. Strangely, I found that the test cases with JDBC steps failed.

Reason for failure :

NoClassDefFoundError: java/sql/SqlFeatureNotSupportedException

After some research online, this is what I found out.

I was using Jdbc drivers for Java 6 and I was compiling my code with Java 5. So changing my project settings to use Java 6 libraries and making the Java compiler as Java 1.6 Compiler did the trick.

I hope this helps someone out there. Let me know, If I made your day.

Screencast Softwares for Mac OSX

Hi All,

I was working on doing some Proof of concept for Functional Testing using SOAP UI Pro. I have got the Proof of Concept ready, But I was not leaning towards another long meeting, where people get tired of listening. So I got an idea of creating some screencasts of what I did. So that it will help our QA guys get started soon. And, Also this was the first time, I was trying to do some screen cast, So I’m excited.

  • Jing (Free Version – 5 Minutes videos only allowed -Allows you to create only SWF file (sigh) )
  • Copernius (No Audio)
  • ScreenCast-O-Matic (Yet to Try)

Since I use Mac OSX, the screen cast softwares that was available to me was  Jing and Copernicus.

I started using Jing and later realized, it allows me to create only 5 minutes videos in the free version of the software. I was not happy, But, I created small parts of the video, which might help viewers who have short attention spans. :) . And  also it allows me to create only swf videos (Sigh).

Copernicus was also something that I considered using, But, to my surprise it did not support Audio recordings. I was like, What the hell. :)

Anyways, For a first time novice, Jing solved me the problem of creating screen casts on Mac OSX. There was also another Software, that I did not have to time to check out. It was ScreenCast-O-Matic , its a Java based tool. I should try it, when I have time. May be then I can create some lengthy videos.

When I started searching. This link gave me the heads up on what I can look for .

Installing Ruby on Rails on Mac OS X 10.5

Hi All,
It’s time to learn a new language. I have learnt a lesson the hard way. On many occasions I have faced different problems and solved it. Few days later, I face the same problem. It strikes me at that moment, ” I have seen this ERROR”. or “I know how to do this”. But, I still have to do the research that I did a few days back. So I thought it’s better to document, what I do, so that I can have it as a reference for the future.

My problems aside… Let’s get to the point.

I wanted to learn ruby on rails. oof.. the moment I said this. There were people saying.. oh another Web framework. But, I have done my research and I’m gonna learn Ruby on Rails.
So let’s start.
Where to start?
Installation of Ruby on Mac OS X 10.5 :

You can install Ruby on Rails on Mac OS X using GEM . What is GEM?

Gem is a package manager for Ruby on Rails. In Short its like a exe or dmg for Ruby programming language. All the libraries and sources are installed properly by gem.

Mac comes with Gem installed ( Which I was unaware of, after some reading, I found that out). But, I faced a problem while trying to install ruby with gems.

I issued the following command to install rails.

gem install rails

I got an error stating,

ERROR:  While executing gem ... (Gem::RemoteSourceException)
    HTTP Response 301 fetching http://gems.rubyforge.org/yaml

After doing some research online, I found that, may be my gems installation was out of date. So I decided to install gem.
I downloaded RubyGems1.3.7

1) Unzip the tar.

2) cd path/to/rubygems1.3.7

3) sudo ruby setup.rb

After installing the fresh copy of Ruby gems, I was able to install rails by issuing

sudo gem install rails

Rails ready to go.