TransWikia.com

Django no module named 'main'

Stack Overflow Asked by Seyed Moein Ayyoubzadeh on December 27, 2021

I was working on a Django project on my PC, Then I create a app named "register" and I’m getting a weird error when I try to run manage.py runserver:
im using Python 3.8.3.
this error occurs when i added forms for registration.

  File "manage.py", line 21, in <module>
    main()
  File "manage.py", line 17, in main    
    execute_from_command_line(sys.argv)
  File "C:Python38libsite-packagesdjangocoremanagement__init__.py", line 401, in execute_from_command_line
    utility.execute()
  File "C:Python38libsite-packagesdjangocoremanagement__init__.py", line 377, in execute
    django.setup()
  File "C:Python38libsite-packagesdjango__init__.py", line 24, in setup
    apps.populate(settings.INSTALLED_APPS)
  File "C:Python38libsite-packagesdjangoappsregistry.py", line 91, in populate
    app_config = AppConfig.create(entry)
  File "C:Python38libsite-packagesdjangoappsconfig.py", line 116, in create
    mod = import_module(mod_path)
  File "C:Python38libimportlib__init__.py", line 127, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 961, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "<frozen importlib._bootstrap>", line 1014, in _gcd_import
  File "<frozen importlib._bootstrap>", line 991, in _find_and_load
  File "<frozen importlib._bootstrap>", line 973, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'main'

my settings.py:

DEBUG = True


ALLOWED_HOSTS = ['*']

SETTINGS_PATH = os.path.dirname(os.path.dirname(__file__))
# Application definition

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'simple2',
    "crispy_forms",
    'main.apps.MainConfig',
    'register.apps.RegisterConfig',
    'register'
]
...


One Answer

I'm guessing that you didn't configure your WSGI app as Django's App engine looks for an app variable in a file named main.py. Here are two ways you can look at this:

Include your WSGI Application

You still can create a file named main.py that includes an app variable. You don't need to write anything, just import it (let's say your project name is projekt:

from projekt.wsgi import main as app

Adding a custom entrypoint

As in Google's Appengine:

entrypoint: Optional. The command that is executed when your app starts. For your app to receive HTTP requests, entrypoint should contain a command which starts a web server that listens on the port specified by the PORT environment variable. If you do not specify an entrypoint, App Engine will configure and start the Gunicorn webserver.

A comparison between the default and your need:

# default
entrypoint: gunicorn -b :$PORT main:app
# What you need
entrypoint: gunicorn -b :$PORT demosite.wsgi:main

More information on here. Reference.

Answered by crimsonpython24 on December 27, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP