简体   繁体   中英

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. 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:

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. As soon as I created an empty __init__.py file there, the app ran normally.

__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'.

Hard to answer without more info, but it looks like something is wrong with PYTHONPATH env variable on your Heroku:

  • 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...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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