0

(Updating my question with more information.) My django app is running fine on my dev server. I have a view that pulls from the database using the below line that works fine:

from myapp.models import MyTable

However, if I add the above 'from/import' to another module (see below structure, it's the module named 'problem_module.py') I'm writing where I want to pull from the sqlite3 database, I get this error.

raise ImportError("Could not import settings '%s' (Is it on sys.path?): %s" % (self.SETTINGS_MODULE, e))
ImportError: Could not import settings 'myfolder.settings' (Is it on sys.path?): No module  named myfolder.settings

I've read and tried various solutions recommended when people get this error, but I missing something because i'm unable to resolve it.

I'm using Django 1.4 and have the lay-out as recommended.

    mysite/
         manage.py

         mysite/
            __init__.py
            settings.py
            urls.py
            wsgi.py
         myapp/
            __init__.py
            models.py
            admin.py
            views.py

            indevelopment/
                __init__.py
                problem_module.py

4 Answers 4

2

I figured out what was happening and why after going through the traceback carefully and looking at the django source code. Here is what happens.

When you run: python manage.py runserver

the environment variable gets set properly assuming you already changed this small little file or just don't pay attention to it because django 1.4 automatically configures it for you. os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myapp.settings")

However, because this setting of os.environ is under a: if __name__ = "__main__" expression, it only gets run if call that file directly, as you do with: python manage.py runserver

Otherwise, if you are running a file that needs that environment variable - say testing a module in Eclipse - , the os.environ needs to get set in another place (shell, etc).

All the that I got generally pointed to this but I needed the context.

But as a little adjustment (yes, not a good idea as it couples) on the source code you can also hardcode it in manually in/django/conf/__init__.py

Specifically to see where it happens, the change below works:

# in module: /django/conf/__init__.py

class LazySettings(LazyObject):
    def _setup(self):
        try:
            # Comment out the call to os.environ and hardcode in your app settings
            #    settings_module = os.environ[ENVIRONMENT_VARIABLE]
            # WARNING: bad practice to do this. ;.
            settings_module = "myapp.settings"
Sign up to request clarification or add additional context in comments.

Comments

0

Have you changed/set DJANGO_SETTINGS_MODULE?

Try export DJANGO_SETTINGS_MODULE=mysite.settings and start your dev server.

1 Comment

I'm pretty sure my environ variable DJANGO_SETTINGS_MODULE is fine. It's set in my manage.py file.
0

modify your manage.py:

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "mysite.settings")

3 Comments

Thanks for suggestion. I don't think it is this. My settings are correct as I initiated startapp from django 1.4. #!/usr/bin/env python import os import sys if __name__ == "__main__": os.environ.setdefault("DJANGO_SETTINGS_MODULE", "myapp.settings") from django.core.management import execute_from_command_line execute_from_command_line(sys.argv)
@wintour you settings.py in myapp folder or mysite folder? Probably you need to recheck.
@Rohan Thanks for the feedback. I updated my answer (twice) and have the accurate tree representation of my folders. My app is working. And the import from models.py is working in views.py, but not in another module in a different folder.
0

PyCharm sometimes override DJANGO_SETTINGS_MODULE to empty string. Try to debug your manage.py and see if it realy changes after setdefault() call. If its not either change pycharm settings or use os.environ['DJANGO....']='my_settings'.. or hack files at .idea/. .idea/workspaed.xml contains

env name="DJANGO_SETTINGS_MODULE" value=""
in this case

Comments

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.