ModuleNotFoundError: No module named 'polls'

I am following the tutorial for Django. I am stuck on the last part of the documentation page: resuable_apps.

The package/module I want to use is

/django-polls/ in /mysite/

When I am trying to make the app pluggable by removing it from the project sub-directory to a directory of the same level something goes wrong.

My file hierarchy is as follows:

django-polls mysite

./django-polls:
LICENSE			docs
MANIFEST.in		polls
README.rst		setup.cfg
dist			setup.py
django_polls.egg-info

    ./django-polls/dist:
    django-polls-0.1.tar.gz

    ./django-polls/django_polls.egg-info:
    PKG-INFO		dependency_links.txt
    SOURCES.txt		top_level.txt

    ./django-polls/docs:

    ./django-polls/polls:
    __init__.py	migrations	urls.py
    __pycache__	models.py	views.py
    admin.py	static
    apps.py		tests.py
    
        ./django-polls/polls/__pycache__:
        __init__.cpython-38.pyc	tests.cpython-38.pyc
        admin.cpython-38.pyc	urls.cpython-38.pyc
        apps.cpython-38.pyc	views.cpython-38.pyc
        models.cpython-38.pyc

    ./django-polls/polls/migrations:
    0001_initial.py	__init__.py	__pycache__

        ./django-polls/polls/migrations/__pycache__:
        0001_initial.cpython-38.pyc
        __init__.cpython-38.pyc

        ./django-polls/polls/static:
        polls

            ./django-polls/polls/static/polls:
            images		style.css

                ./django-polls/polls/static/polls/images:
                background.gif

./mysite:
db.sqlite3	mysite
manage.py	templates

    ./mysite/mysite:
    __init__.py	asgi.py		urls.py
    __pycache__	settings.py	wsgi.py

        ./mysite/mysite/__pycache__:
        __init__.cpython-38.pyc	urls.cpython-38.pyc
        settings.cpython-38.pyc	wsgi.cpython-38.pyc

    ./mysite/templates:
    admin	polls

        ./mysite/templates/admin:
        base_site.html

        ./mysite/templates/polls:
        detail.html	index.html	results.html

When I run ‘python manage.py runserver’ in the /mysite/ directory I get back “ModuleNotFoundError: No module named ‘polls’.”

My mysite/settings.py file is:

Django settings for mysite project.

Generated by 'django-admin startproject' using Django 3.1.5.

For more information on this file, see
https://docs.djangoproject.com/en/3.1/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/3.1/ref/settings/
"""

from pathlib import Path

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent


# Quick-start development settings - unsuitable for production
# See https://docs.djangoproject.com/en/3.1/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = '#lhl+l#0!)tl1f$a2r#_n^la4t%bic4x)=ro0$h0dbd*y@yf@s'

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = [
#	'polls.apps.PollsConfig',
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
	'polls',
]

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 = 'mysite.urls'

TEMPLATES = [
    {
        'BACKEND': 'django.template.backends.django.DjangoTemplates',
        'DIRS': [BASE_DIR / 'templates'],
        '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 = 'mysite.wsgi.application'


# Database
# https://docs.djangoproject.com/en/3.1/ref/settings/#databases

DATABASES = {
    'default': {
        'ENGINE': 'django.db.backends.sqlite3',
        'NAME': BASE_DIR / 'db.sqlite3',
    }
}


# Password validation
# https://docs.djangoproject.com/en/3.1/ref/settings/#auth-password-validators

AUTH_PASSWORD_VALIDATORS = [
    {
        'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
    },
    {
        'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
    },
]


# Internationalization
# https://docs.djangoproject.com/en/3.1/topics/i18n/

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'America/Los_Angeles'

USE_I18N = True

USE_L10N = True

USE_TZ = True


# Static files (CSS, JavaScript, Images)
# https://docs.djangoproject.com/en/3.1/howto/static-files/

STATIC_URL = '/static/'

(django-polls/polls/):
My apps.py is:

from django.apps import AppConfig


class PollsConfig(AppConfig):
    name = 'polls'

If you have finished the documentation tutorial, please let me know how how get past the last part where we move the /polls/ directory into the /django-polls/ directory! Thank you very much.

Unfortunately, the formatting is making it difficult to understand your directory layout. Can you enclose that with the backticks (```) like you’ve done for your code?

I have fixed the formatting. Thank you for your interest and time to help me solve this problem!

So after you’ve moved your project to your other directory, the tutorial goes through the steps of you creating a package and then installing it. After it has been installed, you should see it listed when you do a pip list. Does your package show up?
(It looks to me like you are about at the step for Using your own package)

If you have gone through the installation process but are not seeing the package listed, double and triple check the files you created in Packaging your app. Also make sure that if you’re using a virtual environment for your app, that your virtual environment is active when you install your package, and that you’re checking pip list while that venv is active.

If you’re still having problems, it would be helpful if you posted the contents of the files created in the Packaging your app step - setup.cfg, setup.py, and MANIFEST.in.

I have resolved the issue! The problem was

python -m pip install --user django-polls/dist/django-polls-0.1.tar.gz

Which gave me the error:

ERROR: Can not perform a '--user' install. User site-packages are not visible in this virtualenv.

Which prompted me to just to do a normal

python -m pip install django-polls/dist/django-polls-0.1.tar.gz

Now everything works as intended!

Thank you very much for the help and guidance. It has been very satisfying after many failed attemps to have it work. I noticed your response on a similar problem to mine on the forum. Thank you again for your contribution to the community. It has been a pleasure learning from you.

1 Like