2

Dear Bros, I like Django ORM very much, so I'm trying to use it outside of Django Project itself. But I'm facing a problem when I trying to use it. Here is my file structure on my Mac:

testORM  
   |-- orm
        |-- __init__.py
        |-- models.py  
   |-- manage.py  
   |-- settings.py  
   |-- test.py

Here are my very simple codes:

models.py:

from django.db import models

class test(models.Model):
    name = models.CharField(max_length=50)
    class Meta:
        db_table='test'
        app_label='orm'

manage.py:

import os
import sys

if __name__ == "__main__":
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings")

    from django.core.management import execute_from_command_line

    execute_from_command_line(sys.argv)

settings.py:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.postgresql_psycopg2', # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
        'NAME': 'Portal',                      # Or path to database file if using sqlite3.
        'USER': 'postgres',                      # Not used with sqlite3.
        'PASSWORD': 'Cloud',                  # Not used with sqlite3.
        'HOST': 'localhost',                      # Set to empty string for localhost. Not used with sqlite3.
        'PORT': '5432',                      # Set to empty string for default. Not used with sqlite3.
    }
}

# Local time zone for this installation. Choices can be found here:
# http://en.wikipedia.org/wiki/List_of_tz_zones_by_name
# although not all choices may be available on all operating systems.
# In a Windows environment this must be set to your system time zone.
TIME_ZONE = 'America/Chicago'

INSTALLED_APPS = (
    'orm',
)

test.py:

import sys
sys.path.append('/Users/wally/Documents/workspace/testORM')
sys.path.append('/Users/wally/Documents/workspace/testORM/orm')
import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "settings")

from django.core.management import setup_environ
import settings
setup_environ(settings)

Here is what I got when I trying to execute test.py from eclipse:

Traceback (most recent call last):
  File "/Users/wally/Documents/workspace/testORM/test.py", line 14, in <module>
    setup_environ(settings)
  File "/Library/Python/2.7/site-packages/django/core/management/__init__.py", line 433, in setup_environ
    import_module(project_name)
  File "/Library/Python/2.7/site-packages/django/utils/importlib.py", line 35, in import_module
    __import__(name)
ImportError: No module named testORM

But the when I try to execute

python manage.py shell 
from django.db import connection
cursor = connection.cursor()

they all working fine from command line!!! Even when I create models's object and save data is working fine too, everything is working fine through shell.

  • But I cannot figure out why it's not working when I trying to execute them from test.py. I just didn't start to do anything in test.py, just import the settings.
  • I searched the web, someone solve the problem by adding the project's path to "sys.path", as you could see, I did it, adding the hard-coded full path. But still not work...
  • Could someone ever encounter this situationn? I'm really appreciate if someone could help. Thanks in advace.
  • Note: I'm using python2.7 and Django1.4 on my Mac(v10.8)
alex vasi
  • 5,304
  • 28
  • 31
Wally Yu
  • 51
  • 1
  • 6
  • Tried adding a sys.path.append('/Users/wally/Documents/workspace')? – Aea Jan 23 '13 at 06:37
  • I tried just now, still not working :( – Wally Yu Jan 23 '13 at 07:14
  • Is the script running the same python as you when you type 'python' at the command line? – Lennart Regebro Jan 23 '13 at 07:32
  • Yes, it should be the same python, I checked it, they are all python 2.7. Actually if I execute it by command line: "python test.py", it still not working, the same error:( Anyway, thanks for helping. – Wally Yu Jan 23 '13 at 07:47
  • You shouldn't use `setup_environ` as it is deprecated for a while. See also [both good answers to this question](http://stackoverflow.com/questions/10415429/how-can-i-use-the-django-orm-in-my-tornado-application) on how to do this. – Bouke Jan 23 '13 at 07:55

1 Answers1

1

I created all those files to replicate your environment and was able to produce this error.

python test.py started working when I created an empty __init__.py file in testORM folder.

pankaj28843
  • 2,458
  • 17
  • 34
  • Oh, yeah, everthing finally works when I added `__init__.py` unser "testORM" folder. Thanks sooooo much psjinx. How stupid I am to miss that file... – Wally Yu Jan 23 '13 at 07:52
  • 1
    The problem is that you shouldn't be using `setup_environ`, as that tries to import your project path, hence the error you are receiving. I advice against making your project folder a python module, as you will have import problems once the name of your project folder changes. – Bouke Jan 23 '13 at 07:57
  • 1
    +1 for `project folder should not be a python module`. This is not a good practice. – pankaj28843 Jan 23 '13 at 08:00