简体   繁体   English

如何修复“ ImportError:没有名为...的模块”错误

[英]How to fix “ImportError: No module named …” error

I have a working Django app called django_trial_on_mac that I recently moved to GitHub in order to deploy it to heroku in the future. 我有一个工作正常的Django应用程序django_trial_on_mac ,最近移到GitHub,以便将来将其部署到heroku。 I cloned the project to my local GitHub folder on my mac but when I ran the app from the new directory I receive the following error: 我将项目克隆到了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

I have not yet found a solution to this specific case of the error and am hoping someone could explain or point me towards somewhere I can find the answer. 我尚未找到针对这种特定情况的错误的解决方案,希望有人可以向我解释或指出我可以找到答案的地方。 thanks! 谢谢!

The problem turned out to be that I had a non-standard project directory structure. 问题原来是我有一个非标准的项目目录结构。 I was missing an __init__.py file in in my 'django_trial_on_mac' directory. 我在'django_trial_on_mac'目录中缺少__init__.py文件。 As soon as I created an empty __init__.py file there, the app ran normally. 我在那里创建一个空的__init__.py文件后,该应用程序便正常运行。

__init__.py files tell Django that the directory should be treated as a package and since this file did not exist in the 'django_trial_on_mac' directory, it could not import the module 'django_trial_on_mac'. __init__.py文件告诉Django该目录应该被视为一个包,并且由于该文件不存在于'django_trial_on_mac'目录中,因此它无法导入模块'django_trial_on_mac'。

Hard to answer without more info, but it looks like something is wrong with PYTHONPATH env variable on your Heroku: 没有更多信息很难回答,但是Heroku上的PYTHONPATH env变量似乎出了点问题:

  • it does not include your project directory, or 它不包括您的项目目录,或
  • you might have a non-standard project directory structure, or 您可能具有非标准的项目目录结构,或者
  • the problem might be something as simple as uploading the project with its parent directory, and not only the project directory... 问题可能很简单,就像上传带有父目录的项目一样,而不仅仅是项目目录...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM