Karaage-Cluster/karaage — Issue #137: Error: Could not import settings 'conf.settings' (Is it on sys.path?): No module named settings
Author: brianmay
Created: 2012-02-17 01:11:41+00:00
State: open

Just spent ages trying to debug this issue; not sure if we can do anything about it, but just want to document the problem while it is still fresh in my head.

The standard code for command line scripts is

```python
#!/usr/bin/env python
from django.core.management import setup_environ
from kgadmin.conf import settings
setup_environ(settings)
```

setup_environ(settings) is somewhat weird. Basically it finds the path to kgadmin.conf.settings, /home/brian/tree/django/karaage-admin/kgadmin/conf/settings.pyc and turns it into "conf.settings" (not kgadmin.conf.settings), and then sets the DJANGO_SETTINGS_MODULE environment variable to "conf.settings".

This had my puzzled, how does it find "conf.settings"? Easy, it adds "/home/brian/tree/django/karaage-admin/kgadmin/conf/.." to the environment path, imports the "conf" module, and then resets the path back to what it use to be. Then importing "conf.settings" will work.

```python
# Import the project module. We add the parent directory to PYTHONPATH to
# avoid some of the path errors new users can have.
sys.path.append(os.path.join(project_directory, os.pardir))
project_module = import_module(project_name)
sys.path.pop()
```
# Import the project module. We add the parent directory to PYTHONPATH to
# avoid some of the path errors new users can have.
sys.path.append(os.path.join(project_directory, os.pardir))
project_module = import_module(project_name)
sys.path.pop()

The problem in my case is it found another copy of conf.py in my python path before this one, and loaded that instead of the expected on. In fact the conf.py is the default file generated by sphinx. Arrgh.

This might deserve another bug in Django. It is rather unexpected that trying to load kgadmin.conf.settings will cause it to try and load conf.