Metlab
Metlab

Reputation: 69

Why Django doesn't find a modules which PyCharm finds well?

I tries to build my first Django project. I've created 'superlist' project and 'lists' app inside. My project tree:

pycharm_project_folder
    |
     superlist
           |
            lists
            superlist
            manage.py
            ...
    |
     venv

My lists/views.py:

from django.shortcuts import render


def home_page():
    """home page"""
    pass

My superlist/urls.py

from django.urls import path

from superlist.lists import views

urlpatterns = [
    path('/', views.home_page, name='home')
    # path('admin/', admin.site.urls),
]

My lists/test.py

from django.test import TestCase
from django.urls import resolve

from superlist.lists.views import home_page


class HomePageTest(TestCase):
    """тест домашней страницы"""

    def test_root_url_resolves_to_home_page_view(self):
        """корневой url преобразуется в представление домашней страницы"""
        found = resolve('/')
        self.assertEqual(found.func, home_page)

So, when I run

python3 manage.py test

I see

ModuleNotFoundError: No module named 'superlist.lists'

I don't understad why I got it because paths were suggested by PyCharm

Upvotes: 1

Views: 59

Answers (2)

Metlab
Metlab

Reputation: 69

So, in the end I just marked superlist folder as a 'source folder' in PyCharm. It resolved my issue

Upvotes: 1

tghw
tghw

Reputation: 25303

With Python3, you will want to use relative imports, especially when you have duplicated package names, like you do here. In superlist/urls.py try:

from .lists import views

This assumes the urls.py file is superlist/urls.py and not superlist/superlist/urls.py. If the latter is true, then it would be:

from .superlist.lists import views

Upvotes: 1

Related Questions