Utkarsh
Utkarsh

Reputation: 41

PyCharm - Django settings honoured in run config but not in python console

While setting up my django project in PyCharms I am having trouble configuring the python shell for django. My project structure is as follows:

- mysite_root
    - deployment
        - ...ansible files
    - mysite
        - __init__.py
        - manage.py
        - mysite
            - __init__.py
            - settings.py
            - local_dev_settings.py
        - app1
            - __init__.py
            - ...other files
        - app2
            - __init__.py
            - ...other files

The Sources root is set to mysite_root/mysite My Run/Debug configuration has the environment variable DJANGO_SETTINGS_MODULE=mysite.local_dev_settings & it works perfectly. Inside the Project settings under Language & Frameworks --> Django the configuration is as follows:

- Django project root -- <path to mysite_root/mysite>
- Settings -- mysite/local_dev_settings.py
- Manage script -- manage.py
- Environment variables -- DJANGO_SETTINGS_MODULE=mysite.local_dev_settings

Now, while launching the Python Console or Manage.py Tasks via Tools, I get this error message:

Traceback (most recent call last):
  File "/Users/utkarsh/.venvs/mysite/lib/python3.5/site-packages/IPython/core/interactiveshell.py", line 2885, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-1-4578708c9793>", line 5, in <module>
    if 'setup' in dir(django): django.setup()
  File "/Users/utkarsh/.venvs/mysite/lib/python3.5/site-packages/django/__init__.py", line 17, in setup
    configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
  File "/Users/utkarsh/.venvs/mysite/lib/python3.5/site-packages/django/conf/__init__.py", line 55, in __getattr__
    self._setup(name)
  File "/Users/utkarsh/.venvs/mysite/lib/python3.5/site-packages/django/conf/__init__.py", line 43, in _setup
    self._wrapped = Settings(settings_module)
  File "/Users/utkarsh/.venvs/mysite/lib/python3.5/site-packages/django/conf/__init__.py", line 99, in __init__
    mod = importlib.import_module(self.SETTINGS_MODULE)
  File "/Users/utkarsh/.venvs/mysite/lib/python3.5/importlib/__init__.py", line 126, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
  File "<frozen importlib._bootstrap>", line 986, in _gcd_import
  File "<frozen importlib._bootstrap>", line 969, in _find_and_load
  File "<frozen importlib._bootstrap>", line 956, in _find_and_load_unlocked
ImportError: No module named 'mysite.local_dev_settings'

Edit: Found the Resolution for the issue Please refer answer below for the resolution of this problem.

Upvotes: 0

Views: 745

Answers (1)

Utkarsh
Utkarsh

Reputation: 41

Found the resolution myself.

The issue was that under Build, Execution, Deployment -> Console -> Django Console both Content & Sources root were included & hence mysite_root/mysite came before mysire_root/mysite/mysite due to which PyCharm failed in loading the settings, even though it should have looked in the latter package as well. Upon disabling the option Add Contents root to PYTHONPATH, everything started working fine. :)

Upvotes: 2

Related Questions