ModuleNotFoundError

Hi all,

I have setup a django project but unfortunately I have a ModuleNotFoundError after I changed project folders…

The following error pops up:
File “C:\Users\Lenovo\AppData\Local\Programs\Python\Python38-32\lib\importlib_init_.py”, line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File “”, line 1014, in _gcd_import
File “”, line 991, in _find_and_load
File “”, line 961, in _find_and_load_unlocked
File “”, line 219, in _call_with_frames_removed
File “”, line 1014, in _gcd_import
File “”, line 991, in _find_and_load
File “”, line 973, in _find_and_load_unlocked
ModuleNotFoundError: No module named ‘digital_portfolio’

This is what I have in my project (unfortunately I could only upload one image… ):
folder structure,

Maybe in addition to the ModuleNotFoundError, my “polls” folder say: “Contatins emphasized items”.

Hopefully the topic is clear, please let me know if any additional information is needed.

apps.py

models.py

Instead of trying to upload images, please post code and directory listings as text, between lines consisting only of three backtick ` characters. So you would have a line of ```, then lines of code, then one more line of ```. (Note, be sure you use the backtick ` and not the apostrophe '.)

Briefly, something is trying to import code from a module named digital_portfolio, but I don’t see any module directory by that name in the listing you included. (You should have one by that name at the same level as “mysite” and “polls”.)

1 Like

Hi Ken,

I am sorry for the post.

If I understand you correct, when I change the polls folder to digital_portfolio folder, the issue should be fixed?

I don’t know. We would need to see your settings.py file and your urls.py file to be sure, but changing the name of “polls” to “digital_portfolio” should help with the specific issue you identified. (It may not be the only change needing to be made, however.)

Ken

Thanks for the response Ken,

The code inside the setting.py file below:

    'digital_portfolio',
    
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
]

MIDDLEWARE = [
    'django.middleware.security.SecurityMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.middleware.common.CommonMiddleware',
    'django.middleware.csrf.CsrfViewMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.contrib.messages.middleware.MessageMiddleware',
    'django.middleware.clickjacking.XFrameOptionsMiddleware',
]

ROOT_URLCONF = 'digital_portfolio.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [],
        'APP_DIRS': True,
        'OPTIONS': {
            'context_processors': [
                'django.template.context_processors.debug',
                'django.template.context_processors.request',
                'django.contrib.auth.context_processors.auth',
                'django.contrib.messages.context_processors.messages',
            ],
        },
    },
]

WSGI_APPLICATION = 'digital_portfolio.wsgi.application'

STATIC_URL = '/static/'
STATIC_ROOT = os.path.join(BASE_DIR, 'static')

MEDIA_URL = '/media/'
MEDIA_ROOT = BASE_DIR

The code inside the urls.py file:

from django.contrib import admin
from django.urls import path
import digital_portfolio.views
from django.conf import settings
from django.conf.urls.static import static

urlpatterns = [
    # ADMIN PAGE!
    path('admin/', admin.site.urls),
    # HOMEPAGE! 
    path('', views.index, name='index'),

    # PROFESSIONAL WORK EXPERIENCE PAGE!
    path('professional-work-experiences', digital_portfolio.views.home, name='home'),
    path('jobs-details/<int:job_id>', digital_portfolio.views.detail, name='detail'),
] 
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

Could you maybe explain why you need to see this particular files? As a beginner it is hard to understand the actual meaning of the “polls” folerder and the “templates” folder. I got this information from the official django website.

Like to hear :slightly_smiling_face:

Sure.

settings.py - I specifically wanted to see the INSTALLED_APPS setting - which you show the digital_portfolio app, and what follows it, but not what comes before. I wanted to know if there was anything related to that setting that might need to be addressed.

I also wanted to see your ROOT_URLCONF setting to verify that it’s pointing to the digital_portfolio app.

In your urls.py, I wanted to verify that you’re referencing a digital_portfolio app and nothing else.

So it was mostly to see if there was any other changes I might need to suggest.

The “polls” folder is a django “app”. That directory name must match what’s in your INSTALLED_APPS setting. If you defined an app named digital_portfolio, then that means you must have a directory by that name.
The templates/digital_portfolio directory is the standard location for storing templates for an app by that name. Two directories with different (but coordinated) purposes.

Ken

Thanks, I really appreciate the explanation.

So I have changed the “polls” folder to digital_portfolio. But instead I have the following error:

ModuleNotFoundError: No module named 'digital_portfolio.settings’

Should I add in the “urls.py” file (see below) a code, import digital_portfolio.settings?

from django.contrib import admin
from django.urls import path
import digital_portfolio.views
from django.conf import settings
from django.conf.urls.static import static

urlpatterns = [
    # ADMIN PAGE!
    path('admin/', admin.site.urls),
    # HOMEPAGE! 
    path('', views.index, name='index'),

    # PROFESSIONAL WORK EXPERIENCE PAGE!
    path('professional-work-experiences', digital_portfolio.views.home, name='home'),
    path('jobs-details/<int:job_id>', digital_portfolio.views.detail, name='detail'),
] 
urlpatterns += static(settings.STATIC_URL, document_root=settings.STATIC_ROOT)
urlpatterns += static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

Just as a side note, I’m guessing you’re finding yourself in this situation because you did the tutorial and are trying to rename things for your own project?

I’d suggest in the future that you start projects from scratch. If you followed the tutorial exactly, you started with a manage.py startproject myapp followed by a manage.py startapp polls. You could follow the same pattern, but instead of “polls”, you do manage.py startapp digital_portfolio and rebuild your project from there.

Off the top of my head, I can’t think of everything you might need to do to clean this all up. You might be able to chase this down item by item - or you might not.

I’d recommend starting clean with a new project here.

Ken

1 Like

Hi Ken,

Yes I followed the tutorial as I am a beginner :sweat_smile:.

I have started I new project, probably a good learning process by making a new project :+1:.
I think I will be quit often active on this forum in future haha.

should u not include ‘templates’ inside the DIRS.
THANK YOU

You are right! I faced the same issue and then landed here but I performed everything from scratch.
And learning from tutorials isn’t wrong I belive.
Is there any solution that you could provide without deleting the existing project?
Do you think it’s because of the folder’s and their hierarchy?
Note- I just started learning django yesterday.

<opinion>
Learning from tutorials is great.

Trying to build your project by iteratively modifying an existing tutorial project can be problematic.

If you have actually worked your way through either of the tutorials I reference regularly (the Official Django Tutorial and the Django Girls Tutorial), and understand what you are doing at each and every step along the way, then you are ready to build your own project from scratch.

That way, you’re not trying to figure out what you should keep and what you should delete from the tutorial project, and you don’t create any conflicts between what you want to do and what’s still hanging around.
</opinion>

If you read the complete thread above, you can see that I had to point out about a half-dozen different places where things needed to be checked and verified - just from changing the name of a directory. It’s a whole lot easier to start with things named the way you want them to be.

Thanks, Ken, for your prompt response. I really appreciate that.

I deleted the old one and again created a new project and a new app.
Though this time, I didn’t get that Module error but the challenge now that I’m facing is

After defining the function in the view.py of the “app” that server is not getting generated.

Because after saving, it should automatically take it and run and I also tried runserver , then fom URL but still unable to generate any page apart from the “admin” one.

If you are having a problem, go ahead and open a new topic.

When you do, please remember to copy/paste the relevant code into your message, do not post screen capture images.

Okay, understood. But in this case, there wasn’t any error so I uploaded the screen image.
Were you able to find out my mistake and the issue, please tell.