Django 1.6
Ubuntu 12.04
Python 3.2.3


意外删除了Django应用程序的__pycache__文件夹及其__init__.py文件,并导致Django崩溃。当我python3 manage.py runserver时,它会立即声明名称为agepct的模块,即使该应用程序的目录存在并且所有文件都在其中(我删除的文件除外)也是如此。我清空了垃圾桶,所以无法将文件找回来。除了重新创建整个应用程序之外,还有什么方法可以使应用程序重新运行?

这是我尝试runserver时发出的回溯:

Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python3.2/dist-packages/django/core/management/__init__.py", line 399, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python3.2/dist-packages/django/core/management/__init__.py", line 392, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/local/lib/python3.2/dist-packages/django/core/management/base.py", line 242, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/usr/local/lib/python3.2/dist-packages/django/core/management/base.py", line 280, in execute
    translation.activate('en-us')
  File "/usr/local/lib/python3.2/dist-packages/django/utils/translation/__init__.py", line 130, in activate
    return _trans.activate(language)
  File "/usr/local/lib/python3.2/dist-packages/django/utils/translation/trans_real.py", line 188, in activate
    _active.value = translation(language)
  File "/usr/local/lib/python3.2/dist-packages/django/utils/translation/trans_real.py", line 177, in translation
    default_translation = _fetch(settings.LANGUAGE_CODE)
  File "/usr/local/lib/python3.2/dist-packages/django/utils/translation/trans_real.py", line 159, in _fetch
    app = import_module(appname)
  File "/usr/lib/python3.2/importlib/__init__.py", line 124, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "/usr/lib/python3.2/importlib/_bootstrap.py", line 824, in _gcd_import
    raise ImportError(_ERR_MSG.format(name))
ImportError: No module named agepct

最佳答案

__pycache__目录是字节编译文件的高速缓存,并且在导入包时将重新构建。

另一方面,__init__.py文件是使目录成为模块的原因。如果最初是空的,只需在agepct目录中重新创建它,以使导入再次起作用。请注意,__init__.py不会位于__pycache__目录中。

如果__init__.py不仅仅是一个空的“将此文件打包”文件,那么您必须从头开始重新创建该代码或从备份中恢复该文件。

10-08 08:00