如何修复“ImportError:No module named ...”错误

时间:2015-07-19 11:18:08

标签: python django manage.py

我有一个名为django_trial_on_mac的Django应用程序,我最近转移到GitHub,以便将来部署到heroku。我将项目克隆到我的Mac上的本地GitHub文件夹,但是当我从新目录运行应用程序时,我收到以下错误:

Traceback (most recent call last):    
  File     "/Users/danieloram/GitHub/Python_projects_for_github/django_trial_on_mac/manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/Library/Frameworks/Python.framework/Versions/3.2/lib/python3.2/site-packages/django/core/management/__init__.py", line 338, in execute_from_command_line
    utility.execute()
  File "/Library/Frameworks/Python.framework/Versions/3.2/lib/python3.2/site-packages/django/core/management/__init__.py", line 303, in execute
    settings.INSTALLED_APPS
  File "/Library/Frameworks/Python.framework/Versions/3.2/lib/python3.2/site-packages/django/conf/__init__.py", line 48, in __getattr__
    self._setup(name)
  File "/Library/Frameworks/Python.framework/Versions/3.2/lib/python3.2/site-packages/django/conf/__init__.py", line 44, in _setup
    self._wrapped = Settings(settings_module)
  File "/Library/Frameworks/Python.framework/Versions/3.2/lib/python3.2/site-packages/django/conf/__init__.py", line 92, in __init__
    mod = importlib.import_module(self.SETTINGS_MODULE)
  File     "/Library/Frameworks/Python.framework/Versions/3.2/lib/python3.2/importlib/__init__.py", line 124, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File     "/Library/Frameworks/Python.framework/Versions/3.2/lib/python3.2/importlib/_bootstrap.py", line 807, in _gcd_import
    _gcd_import(parent)
  File     "/Library/Frameworks/Python.framework/Versions/3.2/lib/python3.2/importlib/_bootstrap.py", line 824, in _gcd_import
    raise ImportError(_ERR_MSG.format(name))
ImportError: No module named django_trial_on_mac

我还没有找到解决这个错误的特定情况的解决方案,我希望有人可以解释或指向我某个地方,我可以找到答案。谢谢!

2 个答案:

答案 0 :(得分:2)

问题是我有一个非标准的项目目录结构。我在我的django_trial_on_mac&#39;中错过了__init__.py个文件。目录。只要我在那里创建了一个空的__init__.py文件,该应用就会正常运行。

__init__.py文件告诉Django该目录应该被视为一个包,因为这个文件不存在于'django_trial_on_mac&#39;目录,它无法导入模块&#39; django_trial_on_mac&#39;。

答案 1 :(得分:0)

没有更多信息很难回答,但看起来Heroku上的PYTHONPATH env变量有问题:

  • 它不包含您的项目目录或
  • 您可能有非标准的项目目录结构,或
  • 问题可能就像上传项目及其父目录一样简单,而不仅仅是项目目录......