Wally Yu
Wally Yu

Reputation: 51

Got "ImportError: No module named xxx" when trying to use ORM outside of Django

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.

Upvotes: 2

Views: 1473

Answers (1)

pankaj28843
pankaj28843

Reputation: 2517

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.

Upvotes: 1

Related Questions