设置 Django 时出现问题 - ValueError: 空模块名称

发布于 2024-09-13 03:05:42 字数 2916 浏览 4 评论 0原文

我决定尝试一下 Django(因为我听说过很多关于它的事情)。我正在这里浏览本教程:

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

大约在教程进行到一半时,我被要求从命令行运行此命令:

python manage.pysyncdb

但是,我收到此错误:

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.
    }
}

I've decided to play around a bit with Django (since I've heard so much about it). I'm walking through the tutorial here:

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

about halfway through the tutorial, i'm asked to run this from my command line:

python manage.py syncdb

However, I'm getting this error:

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

here is the relevant part of my 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.
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

眉黛浅 2024-09-20 03:05:42

我收到此错误是因为 settings.py 中列出了不存在的 Django 应用程序名称,在我们的例子中,该名称是空字符串:

INSTALLED_APPS = (
...
''
)

删除这一行解决了问题。

I got this error because of a non-existent Django app name listed in settings.py, which, in our case, was the empty string:

INSTALLED_APPS = (
...
''
)

Removing that one line solved the problem.

放飞的风筝 2024-09-20 03:05:42

看来您的数据库配置不正确。检查你的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?并为其设置一个NAME。

更新:

您在 ENGINE 中有一段额外的时间。将其更改为“django.db.backends.sqlite3”。希望有帮助。

It looks like you don't have your database configured right. Check your settings.py and make sure you have a valid database ENGINE defined:

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.
    }
}

You probably want django.db.backends.sqlite3? And set a NAME for it.

Update:

You have an extra period in ENGINE. Change it to 'django.db.backends.sqlite3'. Hope that helps.

又怨 2024-09-20 03:05:42

在settings.py中的“INSTALLED_APPS”处添加应用程序名称

Add app name at "INSTALLED_APPS" in settings.py

苏别ゝ 2024-09-20 03:05:42

从 Also 中删除空字符串

INSTALLED_APPS = [
    'blog.apps.BlogConfig',
    'users.apps.UsersConfig',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    '' <-- This one
]

也对我有用

Removing an empty string from

INSTALLED_APPS = [
    'blog.apps.BlogConfig',
    'users.apps.UsersConfig',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    '' <-- This one
]

Also worked for me too

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文