0

I just start learning Python3.x from scratch. in the tutorial of django 2.0, I am stuck with this question. please help me to solve the problem.

as a module, the following code works:


[directory]
mydir
  |
  +-test.py
  +-test2.py    
======================

[test.py]
import test2
test2.say()

======================

[test2.py]
def say():
    print("hey")

but in django tutorial_01, (https://docs.djangoproject.com/en/2.0/intro/tutorial01/)

[directry]
polls/
    __init__.py
    admin.py
    apps.py
    migrations/
        __init__.py
    models.py
    tests.py
    urls.py
    views.py

======================

[poll/urls.py]
from django.urls import path
from . import views
urlpatterns = [
    path('', views.index, name='index'),
]

import command does not work without from ., even though views.py exists in the same directory.

Error message says ModuleNotFoundError: No module named 'views'

Could you explain why this kind of error occurs?

Thank you in advance!

Lev Levitsky
  • 63,701
  • 20
  • 147
  • 175
  • What you will not be able to do in your first example is to import `mydir.test` from outside. The linked questions explain that in Python 3 you have to use explicit relative (or absolute) imports for [intra-package references](https://docs.python.org/3/tutorial/modules.html#intra-package-references) (compare to the same part of Python 2 docs). – Lev Levitsky Jan 15 '18 at 23:09
  • Lev-san thank you for your comments !! I will learn English more to be able to find out duplicate post. this time i couldn't find it..... Again, Thank you very much!! –  Jan 16 '18 at 21:11

0 Answers0