Django can't find settings - django

I have a Django project that contains a "config" folder, which has a settings_dev.py file inside of it which is my settings file.
If I try doing anything with manage.py, it complains that it can't find my settings file, even if I explicitly provide it via the --settings option, e.g.
python manage.py syncdb --settings=config.settings_dev
Error: Can't find the file 'settings.py' in the directory containing 'manage.py'. It appears you've customized things.
You'll have to run django-admin.py, passing it your settings module.
However, if I rename my "config" folder to "settings", it works fine. e.g.
python manage.py syncdb --settings=settings.settings_dev
works fine.
What else do I need to specify for it to know that my settings folder is actually named config?

Look into manage.py. It tries to import settings not conf. You have to modify your manage.py file

create a generic settings.py in the project folder that points to the config module. don't forget the __init__.py in the config folder. This might be better than modifying manage.py.
#!/usr/bin/env python
from django.core.management import setup_environ
try:
import config.settings as settings
except ImportError:
import sys
sys.stderr.write("Couldn't find the settings.py module.")
sys.exit(1)
setup_environ(settings)

Have a look at this answer:
This error occurs as well if there is an import error within settings.py.
https://stackoverflow.com/questions/6036599/bizarre-error-importing-settings-in-django#=

Related

Error: Could not import settings

I'm switching a very large multi-package, multi-app Django (1.4.5) project from using Django's native test runner to py.test (2.3.5). The tests for the lowest level package, web-framework, were discovered and run with no errors after creating a setup.cfg (to manage the py.test options) and a conftest.py (to ignore setup.py). When running py.test (with a setup.cfg and conftest.py) on any higher level package, which all require web-framework, I receive the following error:
ERROR: Could not import settings 'high_level_package.test_settings' (Is it on sys.path?): No module named web_framework.settings
I'm using a virtual environment, and web-framework is in the venv at the following location: ENV/lib/python2.7/site-packages/
I've tried with the venv built in the package's root directory and with it built outside the project path, to no avail. Also, I can import web_framework.settings from the Python interactive command line in the higher level package's root directory.
My current conftest.py is just the following line: collect_ignore = ["setup.py"]
I've tried adding the following lines above it:
import os
import sys
sys.path.append(os.path.dirname(__file__))
I also tried hardcoding in the path to the web-framework package in the above sys.path.append.
Neither worked.
In case it's relevant, my setup.cfg is:
[pytest]
python_files = *test*.py
norecursedirs = node_modules ENV
addopts = --junitxml=python_result.xml, --doctest-modules
Edit:
Forgot to mention the traceback relationship. higher_level_package.test_settings imports higher_level_package.settings, which itself imports web_framework.settings.
in order to have it work you either need to have a develop-install of the worktree, or add . to the PYTHONPATH env var
Change to the folder where your main python website module is located
django-admin runserver --settings=mysite.settings
where mysite was created by
django-admin startproject mysite

django-admin.py can't find custom settings file

I have several customized django settings, this is basically my project structure:
MainProject/
manage.py
my_project/
settings/
base.py
dev.py
prod.py
I've created the __init__.py files inside the directories to identify them as packages.
I've exported the DJANGO_SETTINGS_MODULE to point to the chosen settings file.
The manage.py command seems to handle it pretty good, I never had problem with it.
The problem is that no matter what I do the django-admin.py is not able to find any settings file. I've tried several possible solution but nothing seems to work so far.
I've used the --settings=my_project.settings.dev
I've edited and hard-coded the manage.py to let it point to the dev.py file
I've created a settings file either inside the MainProject and my_project directories importing the dev file (that in turn imports the base.py).
I've created a settings file that let Django know which files should it use as settings
This is regarding the point 4:
from django.core.management import setup_environ
try:
import my_project.settings.dev as settings
except ImportError:
import sys
sys.stderr.write("Couldn't find the settings.py module.")
sys.exit(1)
setup_environ(settings)
Nothing seems to work so far.
====================================
SOLUTION:
I did not find the exact solution but thanks to a comment on the chosen answer I understood that you can basically use manage.py for everything that you could do in django-admin.py, I didn't know that! Since things DO work for me using manage.py I'm fine with it.
What I recommend doing:
Create a normal settings.py file and import one of the others in there. This avoids duplication of settings shared among the three scenarios + it is actually the recommended way of doing it according to the DRY principle.
Typically, you will only have the set the debug parameter, database settings and private keys in the specific settings files. All the other settings should be shared among all scenarios to avoid forgetting to update one and getting hard to debug errors.
Have you tried to import the dev settings inside the __init__.py from your settings module?
settings/_init_.py
from .dev import *

Configuration error in django

I am using django with mod_python (I know it is deprecated -- I am just doing this for an exercise), and am getting the following error --
"Could not import project.views. Error was: No module named app.models"
I am getting the 'No module named app.models" in several other places as well. My syncdb is working fine, and if I go into the manage.py shell I can import the models fine. Why is this occurring and what do I need to change? Thank you.
You should use absolute imports everywhere. If your project is structured like so:
/project/settings.py
/project/app/models.py
/project/app/views.py
In INSTALLED_APPS you would use project.app. In app you'd import your models into views: import project.app.models, etc. Alternately you can try adjusting your PYTHONPATH so your imports work. When you run ./manage.py you are in your project folder, and Python automatically adds it to the PYTHONPATH. This doesn't happen automatically in most deployment scenarios (mod_python or other wise).

How do I include a Django app in my PYTHONPATH?

I want to be able to import things from applications in my project without referring to my project name.
My folder structure is like so; I have a project called djangoproject1, and inside I have a folder called apps, and then I have my individual apps in that folder.
djangoproject1, apps, and all my applications have an empty "__init__.py" file in them.
In my settings.py file I have the following code:
import os
import sys
PROJECT_ROOT = os.path.dirname(__file__)
sys.path.insert(0, os.path.join(PROJECT_ROOT, "apps"))
I've tried adding my apps to INSTALLED_APPS several ways:
'djangoproject1.apps.app1',
'djangoproject1.apps.app2',
or
'apps.app1',
'apps.app2',
or
'app1',
'app2',
but nothing works. Whenever I try to do:
from app1 import *
I got an unresolved import error. I'm using the latest versions of eclipse and django
Ok, so I got it to work by adding the apps folder to the PYTHONPATH through eclipse under Project Properties. Is this eclipse only though? I'm not sure how this will work when I try and deploy the site. What do you guys think?
The statement sys.path.insert(0, os.path.join(PROJECT_ROOT, "apps")) looks OK to me. Following this you only need to add app1 to INSTALLED_APPS and everything should work. But apparently they are not working.
Try the following: 1. Print your sys.path and verify that your project's app directory is in the list. 2. Double check that your have an __init__.py inside your apps folder.
Update
OK. Now can you fire up Django shell and try importing again? If it fails, please post the stack trace here.
You must include apps directory to PYTHONPATH. Be sure that in apps directory don't have __init__.py, because it become a package instead of "simple directory". Than, include in settings.py
app1, app2, app3
Use
from app1 import *

Modifing settings in Django

I'm trying to get Django's manage.py to run with modified settings files.
I have three settings files
settings.py
preview.py
live.py
settings.py contains my base settings but on live & preview sites I point the ['DJANGO_SETTINGS_MODULE'] to live or preview which in turn load the base file and any settings specific to that environment.
This works fine, until I try to run migrations using manage.py
I updated the manage.py file to import settings.preview but now when I try to run syncdb or migrate I get this error:
django.core.exceptions.ImproperlyConfigured:
You haven't set the DATABASE_ENGINE
setting yet.
My settings file has this in it though:
DATABASE_ENGINE = 'mysql'
How can I get this to work?
Don't modify manage.py if you can help it. Instead pass it the --settings argument to choose an alternate settings module. Setting up a shell script or alias will make it easier to use this.