edank
edank

Reputation: 191

django no module name 'config'

I wrote these apps using Python 2 and now am using Python 3.

I've tried:

  1. Changing to a new virtualenv and reinstalling requirements
  2. pip install config -> returns a new Syntax error in an Exception...
  3. pip install django-config

When running: python3 manage.py runserver I keep getting the following error:

Traceback (most recent call last):
      File "manage.py", line 22, in <module>
execute_from_command_line(sys.argv)
      File "/usr/local/lib/python3.6/site-packages/django/core/management/__init__.py", line 364, in execute_from_command_line
utility.execute()
      File "/usr/local/lib/python3.6/site-packages/django/core/management/__init__.py", line 308, in execute
settings.INSTALLED_APPS
      File "/usr/local/lib/python3.6/site-packages/django/conf/__init__.py", line 56, in __getattr__
self._setup(name)
      File "/usr/local/lib/python3.6/site-packages/django/conf/__init__.py", line 41, in _setup
self._wrapped = Settings(settings_module)
      File "/usr/local/lib/python3.6/site-packages/django/conf/__init__.py", line 110, in __init__
mod = importlib.import_module(self.SETTINGS_MODULE)
      File "/usr/local/Cellar/python3/3.6.3/Frameworks/Python.framework/Versions/3.6/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 941, in _find_and_load_unlocked
      File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
      File "<frozen importlib._bootstrap>", line 994, in _gcd_import
      File "<frozen importlib._bootstrap>", line 971, in _find_and_load
      File "<frozen importlib._bootstrap>", line 941, in _find_and_load_unlocked
      File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
      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 'config'

Upvotes: 6

Views: 13348

Answers (9)

A01_Aaditya Rathi
A01_Aaditya Rathi

Reputation: 1

Change this line:

django.config.urls

to:

django.conf.urls

Upvotes: 0

questionedbat
questionedbat

Reputation: 1

In your setting.py, change:

from config import db_host, db_database, db_password, db_username, db_port

To:

from .config import db_host, db_database, db_password, db_username, db_port

Upvotes: 0

Eugene
Eugene

Reputation: 93

If using PyCharm, DJANGO_SETTINGS_MODULE variable should be set to project_name.settings. This may be done in Edit Configurations -> Environment variables. See on pic: DJANGO_SETTINGS_MODULE variable configuration to avoid error

Upvotes: 0

Four_0h_Three
Four_0h_Three

Reputation: 612

I had this same problem and to get it to work I had to set the DJANGO_SETTINGS_MODULE environment variable to be the path to my settings.py file

export DJANGO_SETTINGS_MODULE=project_name.settings

Upvotes: 6

sadric adigbe
sadric adigbe

Reputation: 66

I was getting the same error

ModuleNotFoundError: No module named 'chat_room'

Every time I create a new virtual environment and run python -m django I got the same warning. This warning is due to the fact that I set some environment variables for my old app which has the chat_room module. so every time my new virtual environment uses my environment variable set in my .bash_profile instead of the default one in the new Django setting. In the end, after getting rid of that environment variable export DJANGO_SETTINGS_MODULE=chat_room.settings in my .bash_profile everything went ok.

Upvotes: 0

Rachel
Rachel

Reputation: 2894

I had the same issue after reinstalling a working repo onto a new machine. Turned out to be caused by PYTHONPATH, which was unset on the new machine.

Upvotes: 0

TechBrad
TechBrad

Reputation: 87

I received a similar error when copying or cloning my project directory.

ModuleNotFoundError: No module named ''

Creating a new venv wasn't enough for me. I had to delete the old one first, not sure why.

Followed by these commands it worked like a charm after

pip install -r requirements.txt
python manage.py collectstatic

Upvotes: 0

ehacinom
ehacinom

Reputation: 8894

I did something similar to @PetarP:

INSTALLED_APPS = [
    # user-defined
    'blog.apps.BlogConfig',

    # default
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles', 
    # no 'django.contrib.sites'
]

as you can find this at /blog/apps.py:

from django.apps import AppConfig
class BlogConfig(AppConfig):
    name = 'blog'

Upvotes: 2

copser
copser

Reputation: 2641

After you create your module app with python manage.py startapp test_app, you should go into settings.py and register the app into

INSTALLED_APPS = [
    'django.contrib.admin',
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.messages',
    'django.contrib.staticfiles',
    'django.contrib.sites',

    # Register your custom app here
    'test_app',
]

You should read the official django tutorial everything will be explained there.

Upvotes: 1

Related Questions