Reputation: 2813
I followed this link to use Django's ORM on my stand alone application in python. manage.py sql 'application_name' and manage.py syncdb is working well and tables were created when I performed syncdb. The problem I am encountering is when I am running the actual script (for this case, it's dparser.py) that will handle DB transactions, I am encountering a "TypeError: relative imports require the 'package' argument". Below is the stacktrace:
Traceback (most recent call last):
File "dparser.py", line 23, in <module>
from dmodel.models import *
File "/home/<user>/d/dapp/dmodel/models.py", line 1, in <module>
from django.db import models
File "/usr/local/lib/python2.7/site-packages/django/db/__init__.py", line 11, in <module>
if DEFAULT_DB_ALIAS not in settings.DATABASES:
File "/usr/local/lib/python2.7/site-packages/django/utils/functional.py", line 184, in inner
self._setup()
File "/usr/local/lib/python2.7/site-packages/django/conf/__init__.py", line 42, in _setup
self._wrapped = Settings(settings_module)
File "/usr/local/lib/python2.7/site-packages/django/conf/__init__.py", line 93, in __init__
mod = importlib.import_module(self.SETTINGS_MODULE)
File "/usr/local/lib/python2.7/site-packages/django/utils/importlib.py", line 28, in import_module
raise TypeError("relative imports require the 'package' argument")
TypeError: relative imports require the 'package' argument
Below are the contents of my settings.py, models.py and dparser.py:
settings.py:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'd',
'USER': 'root',
'PASSWORD': '<password>',
'HOST': 'localhost',
'PORT': '3306',
}
}
INSTALLED_APPS = ("dapp.dmodel",)
models.py:
from django.db import models
from django.db.models import Max
class Topics(models.Model):
topic_id = models.AutoField(primary_key=True)
topic = models.CharField(max_length=1000)
class Links(models.Model):
link_id = models.AutoField(primary_key=True)
topic = models.ForeignKey(Topics)
link = models.CharField(max_length=1000)
def getLastId(tag):
...
return lastid
dparser.py (partial):
from django.conf import settings
os.environ['DJANGO_SETTINGS_MODULE'] = ".settings.py"
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'd',
'USER': 'root',
'PASSWORD': '<password>',
'HOST': 'localhost',
'PORT': '3306',
}
}
INSTALLED_APPS = ("dapp.dmodel",)
from dmodel.models import * # --> This is were the exception occurs
Hope somebody can help me out here. Thanks in advance!
Upvotes: 4
Views: 4972
Reputation: 8831
Looking at the code of django.utils.importlib.py
where the execption is raised you see that your settings file name is causing the problem:
if name.startswith('.'):
if not package:
raise TypeError("relative imports require the 'package' argument")
Don't use .settings.py
as the name for your settings file, use something that does not start with a '.' and it will solve this particular error.
Upvotes: 9
Reputation: 8831
I'd guess you'd need to do
from dapp.dmodel.models import *
or set the PYTHONPATH
or sys.path
to include the dapp folder.
Upvotes: 0