Django - No module named 'polls'

Asked

Viewed 45 times

0

Hello, I’m starting to study Django (current version: 3.2.5), and I followed this article in an attempt to configure it (version consulted on 07/29/2021): https://www.alura.com.br/artigos/django-instalacao-configuracao-e-escrevendo-seu-primeiro-app

However, when executing the excerpt quoted in the article:

from polls.models import Question, Choice

I get:

Modulenotfounderror: No module named 'polls'

I went over it step by step and I couldn’t find the cause. I searched the Django library website, and there I found a quote:

from . models import Choice, Question

But I couldn’t import it this way either.

I keep thinking that maybe the article is outdated, because in other parts of the article it cited a method that is no longer current in Django, reason why I received notice of 'deprecated'. But maybe I missed some understanding on my part, which wasn’t in the article either, or I’m not seeing where I went wrong.

Does anyone know where the problem/error is?

  • 1

    I’ve never seen anything like this, I suggest it is a course of Alura, you contact the platform, I imagine they can help you in a better way.

  • 1

    It seems that in the post some parts were hidden, this import "from Django.contrib import admin" is from admin.py.. this other import "from polls.models import Question, Choice".. polls looks like a folder.. following the logic Question, Choice is a model that is in models.. I think this is where the example:https://docs.djangoproject.com/pt-br/3.2/intro/tutorial02was copied/

2 answers

0


I found a solution to that part. But I warn you that due to some divergences in the current version of Alura’s article, the next steps also did not work (I believe that instructions/stages were skipped in the article, for example: Classes are cited, which I believe were not created between the steps indicated there (Choice and Question)). Obs.: I launched the question and the request for review of the article in the forum of Alura, but I have not received any feedback, so there are the information below, which may be useful for others who encounter the same problem.

I did several tests and comparisons, until I understood that: from polls.models import Question, Choice #This excerpt looks a lot like the site of the Django project (https://docs.djangoproject.com/pt-br/3.2/intro/tutorial02/), however, there the word "polls" was the name given to the app created. And in the Alura article, the name of the app was cited as the student’s free choice, as excerpt below:

specify the name of the application in the Name field and select OK

After searching and reading the suggestions they mentioned, I understood that when we created an app, using the command python manage.py startapp nome_do_app, it generates a directory at the root of the project, which behaves like a Python package, with Imports and everything.

That is, in the article, where:from polls.models import Question, Choice

The right thing would be: from nome_do_app.models import Question, Choice #Replacing'name_do_app' with the name chosen by the student.

Also, depending on how your project is configured and where your IDE will search python packages for import, you may need to specify the full path, such as:

from aprendendodjango.nome_do_app.models import Question, Choice

Thank you

0

Browser other questions tagged

You are not signed in. Login or sign up in order to post.