语法错误:源代码字符串不能包含空字节

问题描述 投票:0回答:1

我正在尝试运行 python manage.py makemigrations 并抛出以下异常。

请注意 - 1 - 我在 models.py 中有自己的 User 类 2 - 两个模型类,一个在应用程序内部,另一个文件在应用程序外部。外部 models.py 是根据我的数据库架构生成的类。我使用以下命令自动生成文件。

python manage.py inspectdb > models.py

下面是运行时抛出的错误 - python manage.py makemigrations

Traceback (most recent call last):
File "C:\Django-Foodl\Foodl\manage.py", line 22, in <module>
main()
File "C:\Django-Foodl\Foodl\manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "C:\Users\Eswar\AppData\Local\Programs\Python\Python311\Lib\site- 
packages\django\core\management\__init__.py", line 442, in execute_from_command_line
utility.execute()
File "C:\Users\Eswar\AppData\Local\Programs\Python\Python311\Lib\site- 
packages\django\core\management\__init__.py", line 416, in execute
django.setup()
File "C:\Users\Eswar\AppData\Local\Programs\Python\Python311\Lib\site- 
packages\django\__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "C:\Users\Eswar\AppData\Local\Programs\Python\Python311\Lib\site- 
packages\django\apps\registry.py", line 116, in populate
app_config.import_models()
File "C:\Users\Eswar\AppData\Local\Programs\Python\Python311\Lib\site- 
packages\django\apps\config.py", line 269, in import_models
self.models_module = import_module(models_module_name)
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Eswar\AppData\Local\Programs\Python\Python311\Lib\importlib\__init__.py", 
line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
       ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "<frozen importlib._bootstrap>", line 1204, in _gcd_import
File "<frozen importlib._bootstrap>", line 1176, in _find_and_load
File "<frozen importlib._bootstrap>", line 1147, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 690, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 936, in exec_module
File "<frozen importlib._bootstrap_external>", line 1074, in get_code
File "<frozen importlib._bootstrap_external>", line 1004, in source_to_code
File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
SyntaxError: source code string cannot contain null bytes
python django django-rest-framework mysql-python
1个回答
0
投票

我也有同样的问题。这是由 Microsoft Windows 在生成

models.py
时以“UTF-16 LE”编码文件的方式引起的。

解决方案 使用记事本打开文件

models.py
。 单击
Save As
。 使用“保存”按钮右侧的下拉菜单,选择“UTF-8”。请参见下图来找到它。 enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.