You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

创建Django超级用户时遭遇TypeError错误:ctype 'wchar_t'初始化器必须是长度为1的Unicode字符串而非整数

Django 2.0.7 创建超级用户时 TypeError 问题解决

我之前也碰到过这个问题,这是PyPy 3.8 在 Windows 下的 msvcrt 模块兼容性 bug,和老版本 Django 搭配时就容易触发这个错误——具体是 getpass 模块调用 win_getpass 时,向 putwch 传递了整数而非要求的长度为1的Unicode字符串。

先还原下你的操作场景和报错信息:

执行命令:

(venv) PS C:\Users\testuser\Development\Projekte\diwa_portal2\src> python manage.py createsuperuser

交互输入后触发报错:

用户名(留空则使用'testuser'):
电子邮箱地址:
回溯(最近的调用最后发生):
  File "manage.py", line 22, in <module>
    main()
  File "manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "C:\Users\testuser\Development\Projekte\diwa_portal2\venv\lib\site-packages\django\core\management\__init__.py", line 446, in execute_from_command_line
    utility.execute()
  File "C:\Users\testuser\Development\Projekte\diwa_portal2\venv\lib\site-packages\django\core\management\__init__.py", line 440, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "C:\Users\testuser\Development\Projekte\diwa_portal2\venv\lib\site-packages\django\core\management\base.py", line 414, in run_from_argv
    self.execute(*args, **cmd_options)
  File "C:\Users\testuser\Development\Projekte\diwa_portal2\venv\lib\site-packages\django\contrib\auth\management\commands\createsuperuser.py", line 87, in execute
    return super().execute(*args, **options)
  File "C:\Users\testuser\Development\Projekte\diwa_portal2\venv\lib\site-packages\django\core\management\base.py", line 460, in execute
    output = self.handle(*args, **options)
  File "C:\Users\testuser\Development\Projekte\diwa_portal2\venv\lib\site-packages\django\contrib\auth\management\commands\createsuperuser.py", line 171, in handle
    password = getpass.getpass()
  File "C:\Users\testuser\Tools\WPy64-38120\pypy3.8-v7.3.7-win64\Lib\getpass.py", line 103, in win_getpass
    msvcrt.putwch(c)
  File "C:\Users\testuser\Tools\WPy64-38120\pypy3.8-v7.3.7-win64\Lib\msvcrt.py", line 112, in putwch
    _lib._putwch(ord(ch))
TypeError:ctype 'wchar_t'的初始化器必须是长度为1的Unicode字符串,而非整数

给你几个可行的解决办法,按优先级推荐:

  • 最快捷:通过 Django Shell 手动创建超级用户
    绕开命令行交互的密码输入环节,直接用代码创建:

    1. 进入项目的 Django Shell:
      (venv) PS C:\Users\testuser\Development\Projekte\diwa_portal2\src> python manage.py shell
      
    2. 在 Shell 中执行以下代码(替换成你的用户名、邮箱和密码):
      from django.contrib.auth.models import User
      User.objects.create_superuser(username='testuser', email='your@email.com', password='your_secure_password')
      
    3. 输入 exit() 退出 Shell,超级用户就创建完成了。
  • 换用 CPython 环境
    PyPy 虽然性能不错,但在 Windows 下和老版本 Django 的兼容性不如官方的 CPython。如果项目没有强依赖 PyPy,建议切换到对应版本的 CPython 重新搭建虚拟环境,再执行 createsuperuser 命令,大概率能解决问题。

  • 升级 PyPy 或 Django 版本

    • 如果你想继续用 PyPy,可以尝试升级到更近期的 PyPy 版本,新版本可能修复了这个 msvcrt.putwch 的 bug;
    • Django 2.0.7 是比较老的版本,升级到同系列的 LTS 版本(比如 Django 2.2.x),也可能已经处理了这类第三方运行时的兼容性问题。

内容的提问来源于stack exchange,提问作者michacodes

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.28 22:14:06