0

If I have two python modules in a directory main.py and somemodule.py, I can import somemodule by using import somemodule.

./ main.py somemodule.py __init__.py

In django application where we have urls.py and views.py, why won't import views work in this case? But relative import from . import views works?

Mark Evans
  • 974
  • 1
  • 11
  • 29

1 Answers1

0

That's because of python 3 import style and is irrelevant to Django.

Read this for more details: Changes in import statement python3

Community
  • 1
  • 1
Mehdi Pourfar
  • 488
  • 4
  • 13
  • Didn't see the `__init__.py` file in app directory. – Mark Evans Sep 25 '16 at 06:01
  • `__init__.py` is not python3 specific. It should be always present in every python packages. – Mehdi Pourfar Sep 25 '16 at 06:05
  • That still doesn't explain why `import views` doesn't work. I tried putting `__init__.py` along with `main` and `somemodule`, now `import somemodule` works. – Mark Evans Sep 25 '16 at 06:12
  • First, enter the comand `python -V` to see which version of python you're using. Then if you want your code to be both python 2 and python 3 compatible, at the top level of your code write: `from __future__ import absolute_import` and then import your module like this. `from . import submodule` – Mehdi Pourfar Sep 25 '16 at 06:18