Note that there are some explanatory texts on larger screens.

plurals
  1. POGot "ImportError: No module named xxx" when trying to use ORM outside of Django
    text
    copied!<p><strong>Dear Bros,</strong> 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. <strong>Here is my file structure on my Mac:</strong></p> <pre><code>testORM |-- orm |-- __init__.py |-- models.py |-- manage.py |-- settings.py |-- test.py </code></pre> <p><strong>Here are my very simple codes:</strong></p> <p>models.py:</p> <pre><code>from django.db import models class test(models.Model): name = models.CharField(max_length=50) class Meta: db_table='test' app_label='orm' </code></pre> <p>manage.py:</p> <pre><code>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) </code></pre> <p>settings.py:</p> <pre><code>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', ) </code></pre> <p>test.py:</p> <pre><code>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) </code></pre> <p>Here is what I got when I trying to execute test.py from eclipse:</p> <pre><code>Traceback (most recent call last): File "/Users/wally/Documents/workspace/testORM/test.py", line 14, in &lt;module&gt; 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 </code></pre> <p>But the when I try to execute </p> <pre><code>python manage.py shell from django.db import connection cursor = connection.cursor() </code></pre> <p>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.</p> <ul> <li>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.</li> <li>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...</li> <li>Could someone ever encounter this situationn? I'm really appreciate if someone could help. Thanks in advace.</li> <li>Note: I'm using python2.7 and Django1.4 on my Mac(v10.8)</li> </ul>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload