Search code examples
pythondjangopython-3.xdjango-apps

Django Advanced Tutorial: Error when trying run server or migrate app after successfully installing own package


I'm following the Advanced Django Tutorial and have managed to package my polls app into another directory outside of mysite directory. I run pip install --user django-polls/dist/django-polls-0.1.tar.gz inside the directory which my django-polls app is at and manage to successfully install it. However, when I try to run python manage.py runserver inside the mysite directory I get the following message on my terminal:

Traceback (most recent call last):
  File "manage.py", line 15, in <module>
    execute_from_command_line(sys.argv)
  File "/Users/*USER*/django/django/core/management/__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "/Users/*USER*/django/django/core/management/__init__.py", line 357, in execute
    django.setup()
  File "/Users/*USER*/django/django/__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "/Users/*USER*/django/django/apps/registry.py", line 89, in populate
    app_config = AppConfig.create(entry)
  File "/Users/*USER*/django/django/apps/config.py", line 116, in create
    mod = import_module(mod_path)
  File "/Users/*USER*/anaconda3/lib/python3.6/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 994, in _gcd_import
  File "<frozen importlib._bootstrap>", line 971, in _find_and_load
  File "<frozen importlib._bootstrap>", line 953, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'polls.apps'

I tried running python manage.py migrate and get the same message. Could someone explain what's happening here? I'm running the latest version of Django.

UPDATE: these are the apps installed on my settings.py

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

I tried using

INSTALLED_APPS = [
        'polls', ... 

but that just gives another error, No module named 'polls.urls'

EDIT 2: As requested below is my directory schema, I moved the django-polls and mysite folders into a parent directory django-tutorial. However, when running the command python manage.py runserver it replied: /usr/bin/python: No module named django I then reinstalled Django using pip and reinstalled the django-polls module. That seemed to fix the issue. I can now run the python manage.py runserver command and get my project running as before. I have no idea Django was removed from the path put managed to get it back running. Thank you all for the help!

django-tutorial
├── django-polls
│   ├── LICENSE
│   ├── MANIFEST.in
│   ├── README.rst
│   ├── dist
│   │   └── django-polls-0.1.tar.gz
│   ├── django_polls.egg-info
│   │   ├── PKG-INFO
│   │   ├── SOURCES.txt
│   │   ├── dependency_links.txt
│   │   └── top_level.txt
│   ├── docs
│   ├── polls
│   │   ├── __init__.py
│   │   ├── __pycache__
│   │   │   ├── __init__.cpython-36.pyc
│   │   │   ├── admin.cpython-36.pyc
│   │   │   ├── apps.cpython-36.pyc
│   │   │   ├── models.cpython-36.pyc
│   │   │   ├── tests.cpython-36.pyc
│   │   │   ├── urls.cpython-36.pyc
│   │   │   └── views.cpython-36.pyc
│   │   ├── admin.py
│   │   ├── apps.py
│   │   ├── migrations
│   │   │   ├── 0001_initial.py
│   │   │   ├── __init__.py
│   │   │   └── __pycache__
│   │   │       ├── 0001_initial.cpython-36.pyc
│   │   │       └── __init__.cpython-36.pyc
│   │   ├── models.py
│   │   ├── static
│   │   │   └── polls
│   │   │       ├── images
│   │   │       │   └── background.gif
│   │   │       └── style.css
│   │   ├── templates
│   │   │   └── polls
│   │   │       ├── detail.html
│   │   │       ├── index.html
│   │   │       └── results.html
│   │   ├── tests.py
│   │   ├── urls.py
│   │   └── views.py
│   └── setup.py
└── mysite
    ├── db.sqlite3
    ├── django-polls
    │   └── setup.py
    ├── manage.py
    ├── mysite
    │   ├── __init__.py
    │   ├── __pycache__
    │   │   ├── __init__.cpython-36.pyc
    │   │   ├── __init__.cpython-37.pyc
    │   │   ├── settings.cpython-36.pyc
    │   │   ├── settings.cpython-37.pyc
    │   │   ├── urls.cpython-36.pyc
    │   │   ├── urls.cpython-37.pyc
    │   │   ├── wsgi.cpython-36.pyc
    │   │   └── wsgi.cpython-37.pyc
    │   ├── settings.py
    │   ├── urls.py
    │   └── wsgi.py
    ├── polls
    │   └── views.py
    └── templates
        └── admin
            └── base_site.html

Solution

  • What does your settings.py look like? have you added polls into your INSTALLED_APPS?

    INSTALLED_APPS = [
    'polls',
    ....
    ]