问题:
学习github上大佬的python教程(42天)时遇到的问题
django中,为了创建models.py
python manage.py inspectdb > polls/models.py
python manage.py shell
执行这句后就出现了
Traceback (most recent call last):
File "manage.py", line 20, in <module>
main()
File "manage.py", line 16, in main
execute_from_command_line(sys.argv)
File "C:\Users\gg\Desktop\Dive Into Python\.venv\lib\site-packages\django\core\management\__init__.py", line 401, in execute_from_command_line
utility.execute()
File "C:\Users\gg\Desktop\Dive Into Python\.venv\lib\site-packages\django\core\management\__init__.py", line 377, in execute
django.setup()
File "C:\Users\gg\Desktop\Dive Into Python\.venv\lib\site-packages\django\__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "C:\Users\gg\Desktop\Dive Into Python\.venv\lib\site-packages\django\apps\registry.py", line 114, in populate
app_config.import_models()
File "C:\Users\gg\Desktop\Dive Into Python\.venv\lib\site-packages\django\apps\config.py", line 211, in import_models
self.models_module = import_module(models_module_name)
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 994, in _gcd_import
File "<frozen importlib._bootstrap>", line 971, in _find_and_load
File "<frozen importlib._bootstrap>", line 955, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 674, in exec_module
File "<frozen importlib._bootstrap_external>", line 781, in get_code
File "<frozen importlib._bootstrap_external>", line 741, in source_to_code
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
ValueError: source code string cannot contain null bytes
ValueError: source code string cannot contain null bytes
排查结果:
python manage.py inspectdb > polls/models.py
生成出来的文件models.py的文件格式不是utf8导致
解决方法
使用 notepad++ 等编辑器打开 models.py 改成 utf8格式就行了