在Windows下Django syncdb错误(OpenKey错误?)

我试图按照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)
  • 原始configuration是使用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/' 

我有同样的问题,但我解决它不同。 如果以后有其他人有这个问题,上面的解决方案不适合你,请尝试从这个编辑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 

就像我说的,这个答案只是针对任何其他人遇到这个问题,没有成功实施任何其他答案。

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

将解决方法添加到mimetype.py(+/- ln。255):

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

实际上,在将子键名称评估为注册表键名称之前,它会从子键名称中除去所有无效的字符。

不是最好的,但它的工作原理。 可能是特定于此安装。