Django:使用django-admin.py导入错误

时间:2012-02-24 22:21:48

标签: django

目前,我已在/Users/hobbes3/Sites/为Django创建了两个符号链接。 ls -l显示以下内容

lrwxr-xr-x   1 hobbes3  staff    66B Feb 23 04:16 django -> /usr/local/Cellar/python/2.7.2/lib/python2.7/site-packages/django/
lrwxr-xr-x   1 hobbes3  staff    85B Feb 23 01:01 django-admin.py -> /usr/local/Cellar/python/2.7.2/lib/python2.7/site-packages/django/bin/django-admin.py
drwxr-xr-x  15 hobbes3  staff   510B Feb 24 16:04 mysite/

其中mysite/是我唯一的Django项目(根据官方民意调查教程建立)。

当我输入python django-admin.py时,一切正常

hobbes3@hobbes3:~/Sites$ python django-admin.py
Usage: django-admin.py subcommand [options] [args]

Options:
  -v VERBOSITY, --verbosity=VERBOSITY
                        Verbosity level; 0=minimal output, 1=normal output,
                        2=all output
  --settings=SETTINGS   The Python path to a settings module, e.g.
                        "myproject.settings.main". If this isn't provided, the
                        DJANGO_SETTINGS_MODULE environment variable will be
                        used.
  --pythonpath=PYTHONPATH
                        A directory to add to the Python path, e.g.
                        "/home/djangoprojects/myproject".
  --traceback           Print traceback on exception
  --version             show program's version number and exit
  -h, --help            show this help message and exit

Type 'django-admin.py help <subcommand>' for help on a specific subcommand.

Available subcommands:
  cleanup
  compilemessages
  createcachetable
  dbshell
  diffsettings
  dumpdata
  flush
  inspectdb
  loaddata
  makemessages
  reset
  runfcgi
  runserver
  shell
  sql
  sqlall
  sqlclear
  sqlcustom
  sqlflush
  sqlindexes
  sqlinitialdata
  sqlreset
  sqlsequencereset
  startapp
  startproject
  syncdb
  test
  testserver
  validate

但是当我尝试使用inspectdb命令时,我收到导入错误

hobbes3@hobbes3:~/Sites$ python django-admin.py inspectdb doors-eer
Traceback (most recent call last):
  File "django-admin.py", line 5, in <module>
    management.execute_from_command_line()
  File "/usr/local/Cellar/python/2.7.2/lib/python2.7/site-packages/django/core/management/__init__.py", line 429, in execute_from_command_line
    utility.execute()
  File "/usr/local/Cellar/python/2.7.2/lib/python2.7/site-packages/django/core/management/__init__.py", line 379, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/local/Cellar/python/2.7.2/lib/python2.7/site-packages/django/core/management/__init__.py", line 261, in fetch_command
    klass = load_command_class(app_name, subcommand)
  File "/usr/local/Cellar/python/2.7.2/lib/python2.7/site-packages/django/core/management/__init__.py", line 67, in load_command_class
    module = import_module('%s.management.commands.%s' % (app_name, name))
  File "/usr/local/Cellar/python/2.7.2/lib/python2.7/site-packages/django/utils/importlib.py", line 35, in import_module
    __import__(name)
  File "/usr/local/Cellar/python/2.7.2/lib/python2.7/site-packages/django/core/management/commands/inspectdb.py", line 5, in <module>
    from django.db import connections, DEFAULT_DB_ALIAS
  File "/usr/local/Cellar/python/2.7.2/lib/python2.7/site-packages/django/db/__init__.py", line 14, in <module>
    if not settings.DATABASES:
  File "/usr/local/Cellar/python/2.7.2/lib/python2.7/site-packages/django/utils/functional.py", line 276, in __getattr__
    self._setup()
  File "/usr/local/Cellar/python/2.7.2/lib/python2.7/site-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.

我注意到我可以使用python django-admin.py startproject asd启动项目。

我正在运行Python 2.7.2和Django 1.3.1。

1 个答案:

答案 0 :(得分:4)

django-admin.py正在寻找你的settings.py文件,该文件应位于/ Users / hobbes3 / Sites / mysite中。

如果你进入〜/ Sites / mysite然后运行django-admin.py inspectdb门,它应该可以工作。

这假设您在settings.py中设置了DATABASE变量。

此外,在创建项目之后,使用Django的更常规方法是在创建项目后调用manage.py inspectdb。通过调用django-admin.py,环境变量无法正确设置,Django将找不到您的settings.py文件。