Windows下的Django同步错误(OpenKey错误?)

时间:2014-07-29 18:17:46

标签: python django windows apache python-3.4

我正在尝试遵循Django教程(https://docs.djangoproject.com/en/1.6/intro/tutorial01/

我试图跑:

python manage.py syncdb

导致错误:

Traceback (most recent call last):
  File "manage.py", line 10, in <module    

execute_from_command_line(sys.argv)
  File "C:\xampp\python34\lib\site-packages\django\core\management\__init__.py", line 399, in execute_from_command_line

utility.execute()
  File "C:\xampp\python34\lib\site-packages\django\core\management\__init__.py", line 392, in execute

self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\xampp\python34\lib\site-packages\django\core\management\base.py", line 242, in run_from_argv

self.execute(*args, **options.__dict__)
  File "C:\xampp\python34\lib\site-packages\django\core\management\base.py", line 280, in execute

translation.activate('en-us')
  File "C:\xampp\python34\lib\site-packages\django\utils\translation\__init__.py", line 130, in activate

return _trans.activate(language)
  File "C:\xampp\python34\lib\site-packages\django\utils\translation\trans_real.py", line 188, in activate

_active.value = translation(language)
  File "C:\xampp\python34\lib\site-packages\django\utils\translation\trans_real.py", line 177, in translation

default_translation = _fetch(settings.LANGUAGE_CODE)
  File "C:\xampp\python34\lib\site-packages\django\utils\translation\trans_real.py", line 159, in _fetch

app = import_module(appname)
  File "C:\xampp\python34\lib\importlib\__init__.py", line 104, in import_module

return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap    ", line 2231, in _gcd_import
  File "<frozen importlib._bootstrap    ", line 2214, in _find_and_load
  File "<frozen importlib._bootstrap    ", line 2203, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap    ", line 1200, in _load_unlocked
  File "<frozen importlib._bootstrap    ", line 1129, in _exec
  File "<frozen importlib._bootstrap    ", line 1448, in exec_module
  File "<frozen importlib._bootstrap    ", line 321, in _call_with_frames_removed
  File "C:\xampp\python34\lib\site-packages\django\contrib\admin\__init__.py", line 6, in <module    

from django.contrib.admin.sites import AdminSite, site
  File "C:\xampp\python34\lib\site-packages\django\contrib\admin\sites.py", line 4, in <module    

from django.contrib.admin.forms import AdminAuthenticationForm
  File "C:\xampp\python34\lib\site-packages\django\contrib\admin\forms.py", line 6, in <module    

from django.contrib.auth.forms import AuthenticationForm
  File "C:\xampp\python34\lib\site-packages\django\contrib\auth\forms.py", line 17, in <module    

from django.contrib.auth.models import User
  File "C:\xampp\python34\lib\site-packages\django\contrib\auth\models.py", line 17, in <module    

from django.contrib.auth.hashers import (
  File "C:\xampp\python34\lib\site-packages\django\contrib\auth\hashers.py", line 9, in <module    

from django.test.signals import setting_changed
  File "C:\xampp\python34\lib\site-packages\django\test\__init__.py", line 6, in <module    

from django.test.testcases import (TestCase, TransactionTestCase,
  File "C:\xampp\python34\lib\site-packages\django\test\testcases.py", line 25,in <module    

from django.core.servers.basehttp import WSGIRequestHandler, WSGIServer
  File "C:\xampp\python34\lib\site-packages\django\core\servers\basehttp.py", line 16, in <module    

from wsgiref import simple_server
  File "C:\xampp\python34\lib\wsgiref\simple_server.py", line 13, in <module    

from http.server import BaseHTTPRequestHandler, HTTPServer
  File "C:\xampp\python34\lib\http\server.py", line 662, in <module    

class SimpleHTTPRequestHandler(BaseHTTPRequestHandler):
  File "C:\xampp\python34\lib\http\server.py", line 853, in SimpleHTTPRequestHandler

mimetypes.init() # try to read system mime.types
  File "C:\xampp\python34\lib\mimetypes.py", line 348, in init

db.read_windows_registry()
  File "C:\xampp\python34\lib\mimetypes.py", line 255, in read_windows_registry with _winreg.OpenKey(hkcr, subkeyname) as subkey:
  TypeError: OpenKey() argument 2 must be str without null characters or None, not str

环境是:

  • Windows 7(64位)
  • Python 3.4.0(32位)
  • Django(1,6,2,'final',0)
  • Apache(Apache / 2.4.3(Win32)mod_wsgi / 3.5-BRANCH Python / 3.4.0 PHP / 5.3.28)
  • 原始配置是使用XAMPP,安装后安装了mod_wsgi

Settings.py是:

"""
Django settings for DjangoTutorial project.

For more information on this file, see
https://docs.djangoproject.com/en/1.6/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/1.6/ref/settings/
"""

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
import os
BASE_DIR = os.path.dirname(os.path.dirname(__file__))


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/1.6/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '+2)v7j%fz)sc6zf=*(k5wz+vz+(78)8$rezkt7q4!2%z@-ghpr'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

TEMPLATE_DEBUG = True

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = (
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    #'polls',
)

MIDDLEWARE_CLASSES = (
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
)

ROOT_URLCONF = 'DjangoTutorial.urls'

WSGI_APPLICATION = 'DjangoTutorial.wsgi.application'


# Database
# https://docs.djangoproject.com/en/1.6/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.mysql',
        'NAME': 'djangotutorial',
        'USER': 'root',
        'PASSWORD': '',
        'HOST': 'localhost',   # Or an IP Address that your DB is hosted on
        'PORT': '3306',
    }
}

# Internationalization
# https://docs.djangoproject.com/en/1.6/topics/i18n/

LANGUAGE_CODE = 'en-us'

#TIME_ZONE = 'UTC'
TIME_ZONE = 'EST'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/1.6/howto/static-files/

STATIC_URL = '/assets/'

2 个答案:

答案 0 :(得分:2)

我有同样的问题,但我解决了不同的问题。如果将来有其他人遇到此问题并且上述解决方案对您不起作用,请尝试从此处编辑mimetypes.py:

with _winreg.OpenKey(_winreg.HKEY_CLASSES_ROOT, '') as hkcr:
            for subkeyname in enum_types(hkcr):
                try:
                    with _winreg.OpenKey(hkcr, subkeyname) as subkey:
                        # Only check file extensions
                        if not subkeyname.startswith("."):
                            continue

到此:

 with _winreg.OpenKey(_winreg.HKEY_CLASSES_ROOT, '') as hkcr:
            for subkeyname in enum_types(hkcr):
                try:
                    if '\0' in subkeyname:                            #ADD THIS LINE
                        print("Skipping bad key: %s" % subkeyname)    #ADD THIS LINE
                        continue                                      #ADD THIS LINE
                    with _winreg.OpenKey(hkcr, subkeyname) as subkey:
                        # Only check file extensions
                        if not subkeyname.startswith("."):
                            continue

就像我说的那样,这个答案只适用于遇到此问题的其他人,并没有成功实施任何其他答案。

答案 1 :(得分:0)

问题在于我的Windows安装本身。有一些密钥具有无效字符,mimetype.py正在循环查找文件关联。

为mimetype.py(+/- ln.255)添加了变通方法:

**ln 252**
import re

**ln. 255**
pattern = re.compile('[\W{}\[\]_]+')
subkeyname = pattern.sub('',subkeyname)

实际上,它会在将其作为注册表项名称进行评估之前对子角色名称中的所有无效字符进行条带化。

不是最好的,但它有效。并且可能特定于此安装。