Django manage.py 命令在 Cronjob 中生成错误
Django manage.py commands generate error in Cronjob
每当我 运行 执行 manage.py 命令(例如迁移、运行服务器)时,一切都很好。
我正在使用以下 Cronjob 命令:
* * * * * python3 /home/ec2-user/Project/manage.py migrate
但是,每当我在 Crontab 中安排 manage.py 命令时,就会出现以下错误:
File "/home/ec2-user/project/manage.py", line 22, in <module>
main()
File "/home/ec2-user/project/manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "/home/ec2-user/.local/lib/python3.7/site-packages/django/core/management/__init__.py", line 419, in execute_from_command_line
utility.execute()
File "/home/ec2-user/.local/lib/python3.7/site-packages/django/core/management/__init__.py", line 395, in execute
django.setup()
File "/home/ec2-user/.local/lib/python3.7/site-packages/django/__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "/home/ec2-user/.local/lib/python3.7/site-packages/django/apps/registry.py", line 114, in populate
app_config.import_models()
File "/home/ec2-user/.local/lib/python3.7/site-packages/django/apps/config.py", line 301, in import_models
self.models_module = import_module(models_module_name)
File "/usr/lib64/python3.7/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 728, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/home/ec2-user/.local/lib/python3.7/site-packages/django/contrib/auth/models.py", line 3, in <module>
from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
File "/home/ec2-user/.local/lib/python3.7/site-packages/django/contrib/auth/base_user.py", line 48, in <module>
class AbstractBaseUser(models.Model):
File "/home/ec2-user/.local/lib/python3.7/site-packages/django/db/models/base.py", line 122, in __new__
new_class.add_to_class('_meta', Options(meta, app_label))
File "/home/ec2-user/.local/lib/python3.7/site-packages/django/db/models/base.py", line 326, in add_to_class
value.contribute_to_class(cls, name)
File "/home/ec2-user/.local/lib/python3.7/site-packages/django/db/models/options.py", line 207, in contribute_to_class
self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())
File "/home/ec2-user/.local/lib/python3.7/site-packages/django/utils/connection.py", line 15, in __getattr__
return getattr(self._connections[self._alias], item)
File "/home/ec2-user/.local/lib/python3.7/site-packages/django/utils/connection.py", line 62, in __getitem__
conn = self.create_connection(alias)
File "/home/ec2-user/.local/lib/python3.7/site-packages/django/db/utils.py", line 204, in create_connection
backend = load_backend(db['ENGINE'])
File "/home/ec2-user/.local/lib/python3.7/site-packages/django/db/utils.py", line 111, in load_backend
return import_module('%s.base' % backend_name)
File "/usr/lib64/python3.7/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "/home/ec2-user/.local/lib/python3.7/site-packages/django/db/backends/sqlite3/base.py", line 73, in <module>
check_sqlite_version()
File "/home/ec2-user/.local/lib/python3.7/site-packages/django/db/backends/sqlite3/base.py", line 69, in check_sqlite_version
'SQLite 3.9.0 or later is required (found %s).' % Database.sqlite_version
django.core.exceptions.ImproperlyConfigured: SQLite 3.9.0 or later is required (found 3.7.17).
我已经将 SQLite 更新到 3.3.38,Python shell 确认:
Python 3.7.10 (default, Jun 3 2021, 00:02:01)
[GCC 7.3.1 20180712 (Red Hat 7.3.1-13)] on linux
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> import sqlite3
>>> sqlite3.sqlite_version
'3.38.5'
很遗憾,我无法找到问题的根源。然而,将我的 Django 版本降级到 2.0 确实解决了这个问题,因为这个版本的 Django 没有验证 SQLite3 版本。
每当我 运行 执行 manage.py 命令(例如迁移、运行服务器)时,一切都很好。 我正在使用以下 Cronjob 命令:
* * * * * python3 /home/ec2-user/Project/manage.py migrate
但是,每当我在 Crontab 中安排 manage.py 命令时,就会出现以下错误:
File "/home/ec2-user/project/manage.py", line 22, in <module>
main()
File "/home/ec2-user/project/manage.py", line 18, in main
execute_from_command_line(sys.argv)
File "/home/ec2-user/.local/lib/python3.7/site-packages/django/core/management/__init__.py", line 419, in execute_from_command_line
utility.execute()
File "/home/ec2-user/.local/lib/python3.7/site-packages/django/core/management/__init__.py", line 395, in execute
django.setup()
File "/home/ec2-user/.local/lib/python3.7/site-packages/django/__init__.py", line 24, in setup
apps.populate(settings.INSTALLED_APPS)
File "/home/ec2-user/.local/lib/python3.7/site-packages/django/apps/registry.py", line 114, in populate
app_config.import_models()
File "/home/ec2-user/.local/lib/python3.7/site-packages/django/apps/config.py", line 301, in import_models
self.models_module = import_module(models_module_name)
File "/usr/lib64/python3.7/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 1006, in _gcd_import
File "<frozen importlib._bootstrap>", line 983, in _find_and_load
File "<frozen importlib._bootstrap>", line 967, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 728, in exec_module
File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
File "/home/ec2-user/.local/lib/python3.7/site-packages/django/contrib/auth/models.py", line 3, in <module>
from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
File "/home/ec2-user/.local/lib/python3.7/site-packages/django/contrib/auth/base_user.py", line 48, in <module>
class AbstractBaseUser(models.Model):
File "/home/ec2-user/.local/lib/python3.7/site-packages/django/db/models/base.py", line 122, in __new__
new_class.add_to_class('_meta', Options(meta, app_label))
File "/home/ec2-user/.local/lib/python3.7/site-packages/django/db/models/base.py", line 326, in add_to_class
value.contribute_to_class(cls, name)
File "/home/ec2-user/.local/lib/python3.7/site-packages/django/db/models/options.py", line 207, in contribute_to_class
self.db_table = truncate_name(self.db_table, connection.ops.max_name_length())
File "/home/ec2-user/.local/lib/python3.7/site-packages/django/utils/connection.py", line 15, in __getattr__
return getattr(self._connections[self._alias], item)
File "/home/ec2-user/.local/lib/python3.7/site-packages/django/utils/connection.py", line 62, in __getitem__
conn = self.create_connection(alias)
File "/home/ec2-user/.local/lib/python3.7/site-packages/django/db/utils.py", line 204, in create_connection
backend = load_backend(db['ENGINE'])
File "/home/ec2-user/.local/lib/python3.7/site-packages/django/db/utils.py", line 111, in load_backend
return import_module('%s.base' % backend_name)
File "/usr/lib64/python3.7/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "/home/ec2-user/.local/lib/python3.7/site-packages/django/db/backends/sqlite3/base.py", line 73, in <module>
check_sqlite_version()
File "/home/ec2-user/.local/lib/python3.7/site-packages/django/db/backends/sqlite3/base.py", line 69, in check_sqlite_version
'SQLite 3.9.0 or later is required (found %s).' % Database.sqlite_version
django.core.exceptions.ImproperlyConfigured: SQLite 3.9.0 or later is required (found 3.7.17).
我已经将 SQLite 更新到 3.3.38,Python shell 确认:
Python 3.7.10 (default, Jun 3 2021, 00:02:01)
[GCC 7.3.1 20180712 (Red Hat 7.3.1-13)] on linux
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> import sqlite3
>>> sqlite3.sqlite_version
'3.38.5'
很遗憾,我无法找到问题的根源。然而,将我的 Django 版本降级到 2.0 确实解决了这个问题,因为这个版本的 Django 没有验证 SQLite3 版本。