1

My code is here. I have tried different approach from stackoverflow and non of them worked.

import os
import sys
from django.conf import settings

sys.path.append('/var/www/iaas/horizon')
sys.path.append('/var/www/iaas/horizon/openstack_dashboard')
os.environ['DJANGO_SETTINGS_MODULE'] = 'openstack_dashboard.settings'

from bill.models import MonthlyBills
from django.contrib.auth import models

If I run python daemonize.py, here is the error message I get. I am confused because I have already included my django project path in my sys.path

raise ImportError("Could not import settings '%s' (Is it on sys.path?): %s" % (self.SETTINGS_MODULE, e))
ImportError: Could not import settings 'openstack_dashboard.settings' (Is it on sys.path?): cannot import name connection

What I am trying to achieve is to create a python-daemon, I need to have an access in my django models.

I hope someone who could point me where I am mistaking here.

1 Answer 1

1

You have to set up os.environ['DJANGO_SETTINGS_MODULE'] before you import settings.

The process of importing django.conf.settings will look to see if the DJANGO_SETTINGS_MODULE environment variable is set before determining white settings to load.

import os
import sys

sys.path.append('/var/www/iaas/horizon')
sys.path.append('/var/www/iaas/horizon/openstack_dashboard')
os.environ['DJANGO_SETTINGS_MODULE'] = 'openstack_dashboard.settings'

from django.conf import settings
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks Austin for answering, I did what you have suggested but the issue still there. I am can't still import any django models in my script, the error message is the same. ImportError: Could not import settings 'openstack_dashboard.settings' (Is it on sys.path?): cannot import name auth
What I need to do is to have access in my django models for my python-daemon script
Is your script running inside the same environment as the django instance?
My script is located in my django project. But I am running the script separately. I use "python /var/www/iaas/horizon/collector/daemon.py run" to run the daemon script
@DjangoBot Put a print sys.path before importing Django settings to verify that your path is correct. This error could also occur if more than one directory named openstack_dashboard is on your sys.path, but one of the directories doesn't contain the settings.py file. Also check that there is an __init__.py in your openstack_dashboard directory.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.