0

I use to run Python 2 and had a fully working Django application. I have now decided to switch to Python 3 and went with 3.6. When I try to run my tests I now get the following stack trace:

Traceback (most recent call last):
  File "/Users/jonathan/Work/GenettaSoft/modeling-web/modeling/manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/Users/jonathan/anaconda/lib/python3.6/site-packages/django/core/management/__init__.py", line 350, in execute_from_command_line
    utility.execute()
  File "/Users/jonathan/anaconda/lib/python3.6/site-packages/django/core/management/__init__.py", line 324, in execute
    django.setup()
  File "/Users/jonathan/anaconda/lib/python3.6/site-packages/django/__init__.py", line 18, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/Users/jonathan/anaconda/lib/python3.6/site-packages/django/apps/registry.py", line 115, in populate
    app_config.ready()
  File "/Users/jonathan/anaconda/lib/python3.6/site-packages/django/contrib/admin/apps.py", line 22, in ready
    self.module.autodiscover()
  File "/Users/jonathan/anaconda/lib/python3.6/site-packages/django/contrib/admin/__init__.py", line 26, in autodiscover
    autodiscover_modules('admin', register_to=site)
  File "/Users/jonathan/anaconda/lib/python3.6/site-packages/django/utils/module_loading.py", line 50, in autodiscover_modules
    import_module('%s.%s' % (app_config.name, module_to_search))
  File "/Users/jonathan/anaconda/lib/python3.6/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 978, in _gcd_import
  File "<frozen importlib._bootstrap>", line 961, in _find_and_load
  File "<frozen importlib._bootstrap>", line 950, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 655, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 678, in exec_module
  File "<frozen importlib._bootstrap>", line 205, in _call_with_frames_removed
  File "/Users/jonathan/Work/GenettaSoft/modeling-web/modeling/licenses/admin.py", line 2, in <module>
    from models import LicenseModel
ModuleNotFoundError: No module named 'models'

Some googling first led me to http://www.daveoncode.com/2017/03/07/how-to-solve-python-modulenotfound-no-module-named-import-error/ but I don't think any of those situations apply --- after all, it was running fine in Python 2. Next I stumbled on https://github.com/jpadilla/django-rest-framework-xml/issues/17 which led me to make sure I had defusedxml==0.5.0 which didn't really help. More desperately I then tried what is explained here ModuleNotFound error in django in Django tastypie web application which didn't really make sense to me but didn't either fix the problem. So now I am running out of ideas so here I am asking you. What's going on and what can I do?

Community
  • 1
  • 1
jonalv
  • 5,706
  • 9
  • 45
  • 64
  • 2
    Did you try using relative imports? `from .models import LicenseModel`? – v1k45 Apr 25 '17 at 07:53
  • Wow, all it took was a dot‽ Is this some new Python 3 thing? – jonalv Apr 25 '17 at 07:58
  • Nope, it is not a new thing in py3, it exists in py2 too. The `.` (dot) tells python use find the module in your local package instead on looking in the site-packages where all the 3rd party libs are installed and stored. Read: [relative and absolute imports in python](http://stackoverflow.com/questions/4209641/absolute-vs-explicit-relative-import-of-python-module) – v1k45 Apr 25 '17 at 08:01
  • Hm, seems it was less picky about that before then? Oh well, it makes sense. – jonalv Apr 25 '17 at 08:02
  • 2
    yes, python3 has enforced explicit relative imports, previously they were implicit. see: [Changes in import statement python3](http://stackoverflow.com/questions/12172791/changes-in-import-statement-python3) – v1k45 Apr 25 '17 at 08:04

0 Answers0