Re: Django environment setup path problem

313 views
Skip to first unread message

Bill Freeman

unread,
Feb 13, 2013, 8:54:52 AM2/13/13
to django...@googlegroups.com
How are you invoking manage.py?  Try cd'ing to the directory containing it before running it and see if that helps.  (That directory will then automatically be on python's sys.path, which is needed.  DJANGO_SETTINGS_MODULE must be a python style package.module style path, not a filesystem path, so depending on what yout mean by "path to project" I wouldn't expect your second approach to work either.)

On Wed, Feb 13, 2013 at 1:57 AM, kulbhushan patariya <kulbhus...@gmail.com> wrote:
Hi,
I have following problem regarding Django.
Can anyone help me out 
-----------------------------------------------------------------------------
kbjp@kbjp-VGN-CS35GN-B:~/Desktop/kbproject/firstjango$ django-admin.py syncdb
Traceback (most recent call last):
  File "/usr/local/bin/django-admin.py", line 5, in <module>
    management.execute_from_command_line()
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 443, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 382, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 261, in fetch_command
    klass = load_command_class(app_name, subcommand)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 69, in load_command_class
    module = import_module('%s.management.commands.%s' % (app_name, name))
  File "/usr/local/lib/python2.7/dist-packages/django/utils/importlib.py", line 35, in import_module
    __import__(name)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/commands/syncdb.py", line 8, in <module>
    from django.core.management.sql import custom_sql_for_model, emit_post_sync_signal
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/sql.py", line 6, in <module>
    from django.db import models
  File "/usr/local/lib/python2.7/dist-packages/django/db/__init__.py", line 11, in <module>
    if DEFAULT_DB_ALIAS not in settings.DATABASES:
  File "/usr/local/lib/python2.7/dist-packages/django/utils/functional.py", line 184, in inner
    self._setup()
  File "/usr/local/lib/python2.7/dist-packages/django/conf/__init__.py", line 40, in _setup
    raise ImportError("Settings cannot be imported, because environment variable %s is undefined." % ENVIRONMENT_VARIABLE)
ImportError: Settings cannot be imported, because environment variable DJANGO_SETTINGS_MODULE is undefined.



Here is the content of manage.py file
--------------------------------------------------------------------------------------------------------------------
#!/usr/bin/env python
import os
import sys

if __name__ == "__main__":
    os.environ.setdefault("DJANGO_SETTINGS_MODULE", "firstjango.settings")

    from django.core.management import execute_from_command_line

    execute_from_command_line(sys.argv)

-----------------------------------------------------------------------------------------------------------------------------------

Here is the error which i received after executing export DJANGO_SETTINGS_MODULE="path to project"


--------------------------------------------------------------------
kbjp@kbjp-VGN-CS35GN-B:~/Desktop/kbproject/firstjango$ export DJANGO_SETTINGS_MODULE=firstjango.settings
kbjp@kbjp-VGN-CS35GN-B:~/Desktop/kbproject/firstjango$ django-admin.py syncdb
Traceback (most recent call last):
  File "/usr/local/bin/django-admin.py", line 5, in <module>
    management.execute_from_command_line()
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 443, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 382, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 261, in fetch_command
    klass = load_command_class(app_name, subcommand)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 69, in load_command_class
    module = import_module('%s.management.commands.%s' % (app_name, name))
  File "/usr/local/lib/python2.7/dist-packages/django/utils/importlib.py", line 35, in import_module
    __import__(name)
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/commands/syncdb.py", line 8, in <module>
    from django.core.management.sql import custom_sql_for_model, emit_post_sync_signal
  File "/usr/local/lib/python2.7/dist-packages/django/core/management/sql.py", line 6, in <module>
    from django.db import models
  File "/usr/local/lib/python2.7/dist-packages/django/db/__init__.py", line 11, in <module>
    if DEFAULT_DB_ALIAS not in settings.DATABASES:
  File "/usr/local/lib/python2.7/dist-packages/django/utils/functional.py", line 184, in inner
    self._setup()
  File "/usr/local/lib/python2.7/dist-packages/django/conf/__init__.py", line 42, in _setup
    self._wrapped = Settings(settings_module)
  File "/usr/local/lib/python2.7/dist-packages/django/conf/__init__.py", line 95, in __init__
    raise ImportError("Could not import settings '%s' (Is it on sys.path?): %s" % (self.SETTINGS_MODULE, e))
ImportError: Could not import settings 'firstjango.settings' (Is it on sys.path?): No module named firstjango.settings
kbjp@kbjp-VGN-CS35GN-B:~/Desktop/kbproject/firstjango$ 

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Yussi

unread,
Feb 13, 2013, 9:20:34 AM2/13/13
to django...@googlegroups.com
I see what you're doing, (It did take me half an hour of staring at your
email though)

The steps are:
$django-admin startproject myproj
$cd myproj
$vi myproj/settings.py
than add your databases
than run
$./manage.py syncdb
not django-admin syncdb

On 13/02/13 13:54, Bill Freeman wrote:
> How are you invoking manage.py? Try cd'ing to the directory containing
> it before running it and see if that helps. (That directory will then
> automatically be on python's sys.path, which is needed.
> DJANGO_SETTINGS_MODULE must be a python style package.module style path,
> not a filesystem path, so depending on what yout mean by "path to
> project" I wouldn't expect your second approach to work either.)
>
> On Wed, Feb 13, 2013 at 1:57 AM, kulbhushan patariya
> <kulbhus...@gmail.com <mailto:kulbhus...@gmail.com>> wrote:
>
> Hi,
> I have following problem regarding Django.
> Can anyone help me out
> ------------------------------__------------------------------__-----------------
> kbjp@kbjp-VGN-CS35GN-B:~/__Desktop/kbproject/firstjango$
> django-admin.py syncdb
> Traceback (most recent call last):
> File "/usr/local/bin/django-admin.__py", line 5, in <module>
> management.execute_from___command_line()
> File
> "/usr/local/lib/python2.7/__dist-packages/django/core/__management/__init__.py",
> line 443, in execute_from_command_line
> utility.execute()
> File
> "/usr/local/lib/python2.7/__dist-packages/django/core/__management/__init__.py",
> line 382, in execute
> self.fetch_command(subcommand)__.run_from_argv(self.argv)
> File
> "/usr/local/lib/python2.7/__dist-packages/django/core/__management/__init__.py",
> line 261, in fetch_command
> klass = load_command_class(app_name, subcommand)
> File
> "/usr/local/lib/python2.7/__dist-packages/django/core/__management/__init__.py",
> line 69, in load_command_class
> module = import_module('%s.management.__commands.%s' %
> (app_name, name))
> File
> "/usr/local/lib/python2.7/__dist-packages/django/utils/__importlib.py",
> line 35, in import_module
> __import__(name)
> File
> "/usr/local/lib/python2.7/__dist-packages/django/core/__management/commands/syncdb.py"__,
> line 8, in <module>
> from django.core.management.sql import custom_sql_for_model,
> emit_post_sync_signal
> File
> "/usr/local/lib/python2.7/__dist-packages/django/core/__management/sql.py",
> line 6, in <module>
> from django.db import models
> File
> "/usr/local/lib/python2.7/__dist-packages/django/db/____init__.py",
> line 11, in <module>
> if DEFAULT_DB_ALIAS not in settings.DATABASES:
> File
> "/usr/local/lib/python2.7/__dist-packages/django/utils/__functional.py",
> line 184, in inner
> self._setup()
> File
> "/usr/local/lib/python2.7/__dist-packages/django/conf/____init__.py", line
> 40, in _setup
> raise ImportError("Settings cannot be imported, because
> environment variable %s is undefined." % ENVIRONMENT_VARIABLE)
> ImportError: Settings cannot be imported, because environment
> variable DJANGO_SETTINGS_MODULE is undefined.
>
>
>
> Here is the content of manage.py file
> --------------------------------------------------------------------------------------------------------------------
> *#!/usr/bin/env python*
> *import os*
> *import sys*
> *
> *
> *if __name__ == "__main__":*
> * os.environ.setdefault("DJANGO_SETTINGS_MODULE",
> "firstjango.settings")*
> *
> *
> * from django.core.management import execute_from_command_line*
> *
> *
> * execute_from_command_line(sys.argv)*
>
> -----------------------------------------------------------------------------------------------------------------------------------
>
> Here is the error which i received after executing export
> DJANGO_SETTINGS_MODULE=__"path to project"
>
>
> ------------------------------__------------------------------__--------
> kbjp@kbjp-VGN-CS35GN-B:~/__Desktop/kbproject/firstjango$ export
> DJANGO_SETTINGS_MODULE=__firstjango.settings
> kbjp@kbjp-VGN-CS35GN-B:~/__Desktop/kbproject/firstjango$
> django-admin.py syncdb
> Traceback (most recent call last):
> File "/usr/local/bin/django-admin.__py", line 5, in <module>
> management.execute_from___command_line()
> File
> "/usr/local/lib/python2.7/__dist-packages/django/core/__management/__init__.py",
> line 443, in execute_from_command_line
> utility.execute()
> File
> "/usr/local/lib/python2.7/__dist-packages/django/core/__management/__init__.py",
> line 382, in execute
> self.fetch_command(subcommand)__.run_from_argv(self.argv)
> File
> "/usr/local/lib/python2.7/__dist-packages/django/core/__management/__init__.py",
> line 261, in fetch_command
> klass = load_command_class(app_name, subcommand)
> File
> "/usr/local/lib/python2.7/__dist-packages/django/core/__management/__init__.py",
> line 69, in load_command_class
> module = import_module('%s.management.__commands.%s' %
> (app_name, name))
> File
> "/usr/local/lib/python2.7/__dist-packages/django/utils/__importlib.py",
> line 35, in import_module
> __import__(name)
> File
> "/usr/local/lib/python2.7/__dist-packages/django/core/__management/commands/syncdb.py"__,
> line 8, in <module>
> from django.core.management.sql import custom_sql_for_model,
> emit_post_sync_signal
> File
> "/usr/local/lib/python2.7/__dist-packages/django/core/__management/sql.py",
> line 6, in <module>
> from django.db import models
> File
> "/usr/local/lib/python2.7/__dist-packages/django/db/____init__.py",
> line 11, in <module>
> if DEFAULT_DB_ALIAS not in settings.DATABASES:
> File
> "/usr/local/lib/python2.7/__dist-packages/django/utils/__functional.py",
> line 184, in inner
> self._setup()
> File
> "/usr/local/lib/python2.7/__dist-packages/django/conf/____init__.py", line
> 42, in _setup
> self._wrapped = Settings(settings_module)
> File
> "/usr/local/lib/python2.7/__dist-packages/django/conf/____init__.py", line
> 95, in __init__
> raise ImportError("Could not import settings '%s' (Is it on
> sys.path?): %s" % (self.SETTINGS_MODULE, e))
> ImportError: Could not import settings 'firstjango.settings' (Is it
> on sys.path?): No module named firstjango.settings
> kbjp@kbjp-VGN-CS35GN-B:~/__Desktop/kbproject/firstjango$
>
> --
> You received this message because you are subscribed to the Google
> Groups "Django users" group.
> To unsubscribe from this group and stop receiving emails from it,
> send an email to django-users...@googlegroups.com
> <mailto:django-users%2Bunsu...@googlegroups.com>.
> To post to this group, send email to django...@googlegroups.com
> <mailto:django...@googlegroups.com>.

Bill Freeman

unread,
Feb 13, 2013, 1:37:21 PM2/13/13
to django...@googlegroups.com
This is a version 1.4 Django.  Yes?

Try adding to manage.py, between the "if ..." line and the "os.environ..." lines, and indented like the "os.environ..." line:

    import pdb;pdb.set_trace()

Then run:

   $ ./manage.py shell

When it stops at the breakpoint, type:

  p os.environ['DJANGO_SETTINGS_MODULE']

I expect this to complain that there is no such key.  Now type:

  n

to single step over the setdefault line and again type:

  p os.environ['DJANGO_SETTINGS_MODULE']

This time it should be happy and print something like:

  "firstdjango.settings"

Now try typeing (be sure to include the "!"):

  !import firstdjango.settings as s

and if that doesn't raise an error, try:

  pp s.INSTALLED_APPS

If this stuff isn't working do:

  p os.getcwd()
  pp sys.path

Assuming that the current working directory is the one containing manage.py and the like named directory that in turn contains settings.py, then either it must be on sys.path, or an empty string (more likely) will be on sys.path (implying the use of the current working directory).

If everything so far was good, and nobody has a better suggestion, and you want to take the time, then you can use the "m" command again to step over the "from ... import ..." line, and then the "s" command to step into the "execute_from_command_line" function, use "n" to step through it's parts until you find the one that fails, exit, restart, step back to the failing part and step into it, repeat until enlightened.

Bill


    <mailto:django-users%2Bunsu...@googlegroups.com>.

    To post to this group, send email to django...@googlegroups.com

    Visit this group at http://groups.google.com/group/django-users?hl=en.
    For more options, visit https://groups.google.com/groups/opt_out.



--
You received this message because you are subscribed to the Google
Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send

To post to this group, send email to django...@googlegroups.com.
Visit this group at http://groups.google.com/group/django-users?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.


--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages