设置Django时遇到问题 - ValueError:模块名为空

10 投票
4 回答
16145 浏览
提问于 2025-04-16 02:10

我决定试试Django(因为我听说了很多关于它的事情)。我正在按照这里的教程进行学习:

http://docs.djangoproject.com/en/1.2/intro/tutorial01/#intro-tutorial01

在教程进行到一半的时候,我被要求在命令行中运行这个命令:

python manage.py syncdb

但是,我遇到了这个错误:

C:\django-projects\mysite>python manage.py syncdb
Traceback (most recent call last):
  File "manage.py", line 11, in <module>
    execute_manager(settings)
  File "C:\Python25\lib\site-packages\django\core\management\__init__.py", line 438, in execute_manager
    utility.execute()
  File "C:\Python25\lib\site-packages\django\core\management\__init__.py", line 379, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Python25\lib\site-packages\django\core\management\__init__.py", line 257, in fetch_command
    klass = load_command_class(app_name, subcommand)
  File "C:\Python25\lib\site-packages\django\core\management\__init__.py", line 67, in load_command_class
    module = import_module('%s.management.commands.%s' % (app_name, name))
  File "C:\Python25\lib\site-packages\django\utils\importlib.py", line 35, in import_module
    __import__(name)
  File "C:\Python25\lib\site-packages\django\core\management\commands\syncdb.py", line 7, in <module>
    from django.core.management.sql import custom_sql_for_model, emit_post_sync_signal
  File "C:\Python25\lib\site-packages\django\core\management\sql.py", line 5, in <module>
    from django.contrib.contenttypes import generic
  File "C:\Python25\lib\site-packages\django\contrib\contenttypes\generic.py", line 6, in <module>
    from django.db import connection
  File "C:\Python25\lib\site-packages\django\db\__init__.py", line 75, in <module>
    connection = connections[DEFAULT_DB_ALIAS]
  File "C:\Python25\lib\site-packages\django\db\utils.py", line 91, in __getitem__
    backend = load_backend(db['ENGINE'])
  File "C:\Python25\lib\site-packages\django\db\utils.py", line 32, in load_backend
    return import_module('.base', backend_name)
  File "C:\Python25\lib\site-packages\django\utils\importlib.py", line 35, in import_module
    __import__(name)
ValueError: Empty module name

这是我setup.py文件中相关的部分

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3.', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
        'NAME': 'c:/python25/ramysDB',                      # Or path to database file if using sqlite3.
        'USER': '',                      # Not used with sqlite3.
        'PASSWORD': '',                  # Not used with sqlite3.
        'HOST': '',                      # Set to empty string for localhost. Not used with sqlite3.
        'PORT': '',                      # Set to empty string for default. Not used with sqlite3.
    }
}

4 个回答

0

在settings.py文件中,把应用的名字加到“INSTALLED_APPS”里。

14

我遇到这个错误是因为在settings.py文件中列出了一个不存在的Django应用名称,在我们这个例子里,就是一个空字符串:

INSTALLED_APPS = (
...
''
)

删除那一行代码就解决了问题。

6

看起来你的数据库配置有问题。检查一下你的settings.py文件,确保你定义了一个有效的数据库引擎:

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.', # Add 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'oracle'.
        'NAME': '',                      # Or path to database file if using sqlite3.
        'USER': '',                      # Not used with sqlite3.
        'PASSWORD': '',                  # Not used with sqlite3.
        'HOST': '',                      # Set to empty string for localhost. Not used with sqlite3.
        'PORT': '',                      # Set to empty string for default. Not used with sqlite3.
    }
}

你可能想用的是 django.db.backends.sqlite3?记得给它设置一个名称。

更新:

你在ENGINE里多加了一个句号。把它改成'django.db.backends.sqlite3'。希望这能帮到你。

撰写回答