django-configurations: "improperly installed" error when run from PyCharm, yet works on command line

Hi,

I am trying to use django-configurations with a Python3/Django-1.8.8project: everything works fine when run from the command line – e.g., “python manage.py runserver” - yet fails when from inside PyCharm (env variables are set in the run settings pane) as follows:

DJANGO_CONFIGURATION=Dev;DJANGO_SETTINGS_MODULE=myApp.settings;PYTHONUNBUFFERED=1

The frameworks and interpreter are set up using a virtual environment; again, when run from the command line everything is fine.

django-configuration is set up as follows:

wsgi.py:

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myApp.settings')
os.environ.setdefault('DJANGO_CONFIGURATION', 'Dev')

from configurations.wsgi import get_wsgi_application

application = get_wsgi_application()

manage.py:

import os
import sys

if __name__ == "__main__":
    os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'myApp.settings')
    os.environ.setdefault('DJANGO_CONFIGURATION', 'Dev')

    from configurations.management import execute_from_command_line
    execute_from_command_line(sys.argv)

From settings:

import os
from configurations import Configuration, values

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

class Common(Configuration):
#  ...lots of common settings....


class Prod(Common):
    # SECURITY WARNING: don't run with debug turned on in production!
    DEBUG = False
    # AWS/S3 - PRODUCTION
    AWS_S3_SECURE_URLS = False       # use http instead of https
    AWS_QUERYSTRING_AUTH = False     # don't add complex authentication-related query parameters for requests
# ... even more PROD settings... 



class Dev(Common):
    DEBUG = True
    TEMPLATE_DEBUG = DEBUG
    #AWS/S3 - DEVELOPMENT
    AWS_S3_SECURE_URLS = False       # use http instead of https
    AWS_QUERYSTRING_AUTH = False     # don't add complex authentication-related query parameters for requests
# ... development settings... 

Any suggestions would be appreciated…

Console output

/Users/spector/Work/myApp/BackEnd/myAppSensorBackend/env/bin/python /Applications/PyCharm.app/Contents/helpers/pydev/pydevconsole.py 55302 55303
PyDev console: starting.

import sys; print('Python %s on %s' % (sys.version, sys.platform))
import django; print('Django %s' % django.get_version())
sys.path.extend(['/Users/spector/Work/myApp/BackEnd/myAppSensorBackend', '/Users/spector/Work/myApp/BackEnd/myAppSensorBackend/src', '/Applications/PyCharm.app/Contents/helpers/pycharm', '/Applications/PyCharm.app/Contents/helpers/pydev'])
if 'setup' in dir(django): django.setup()
import django_manage_shell; django_manage_shell.run("/Users/spector/Work/myApp/BackEnd/myAppSensorBackend/src")
Python 3.5.1 (default, Jan 22 2016, 08:54:32) 
[GCC 4.2.1 Compatible Apple LLVM 7.0.2 (clang-700.1.81)] on darwin
Django 1.8.8
Traceback (most recent call last):
  File "<input>", line 5, in <module>
  File "/Users/spector/Work/myApp/BackEnd/myAppSensorBackend/env/lib/python3.5/site-packages/django/__init__.py", line 17, in setup
    configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
  File "/Users/spector/Work/myApp/BackEnd/myAppSensorBackend/env/lib/python3.5/site-packages/django/conf/__init__.py", line 48, in __getattr__
    self._setup(name)
  File "/Users/spector/Work/myApp/BackEnd/myAppSensorBackend/env/lib/python3.5/site-packages/django/conf/__init__.py", line 44, in _setup
    self._wrapped = Settings(settings_module)
  File "/Users/spector/Work/myApp/BackEnd/myAppSensorBackend/env/lib/python3.5/site-packages/django/conf/__init__.py", line 92, in __init__
    mod = importlib.import_module(self.SETTINGS_MODULE)
  File "/usr/local/Cellar/python3/3.5.1/Frameworks/Python.framework/Versions/3.5/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 958, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 662, in exec_module
  File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
  File "/Users/spector/Work/myApp/BackEnd/myAppSensorBackend/src/myApp/settings.py", line 132, in <module>
    class Prod(Common):
  File "/Users/spector/Work/myApp/BackEnd/myAppSensorBackend/env/lib/python3.5/site-packages/configurations/base.py", line 32, in __new__
    raise ImproperlyConfigured(install_failure)
django.core.exceptions.ImproperlyConfigured: django-configurations settings importer wasn't correctly installed. Please use one of the starter functions to install it as mentioned in the docs: http://django-configurations.readthedocs.org/



About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Reactions: 5
  • Comments: 30 (2 by maintainers)

Commits related to this issue

Most upvoted comments

I was having the same issue with pycharm, in pycharm settings add the following to django console starting script:

import sys; print('Python %s on %s' % (sys.version, sys.platform))
import django; print('Django %s' % django.get_version())
sys.path.extend([WORKING_DIR_AND_PYTHON_PATHS])

# my addition
import configurations
configurations.setup()
# my addition

if 'setup' in dir(django): django.setup()
import django_manage_shell; django_manage_shell.run(PROJECT_ROOT)

After addling those lines my django console works again.

from configurations import importer

importer.install(check_options=True)

just add this code to the __init__.py of your main django app.

You need no monkey-patching. Error occurs because Pycharm loads django application itself bypassing your manage.py

A more generalized solution to my previous snippets (ignores your install path)

Add in manage.py after imports and before __main section

if sys.argv[0] and sys.argv[0].find('django_test_manage.py'):
    import configurations

    configurations.setup()

Found a hack:

/home/arne/.opt/pycharm-5.0.2/helpers/pycharm/django_test_manage.py
--- /home/arne/.opt/pycharm-5.0.2/helpers/pycharm/django_test_manage.py.bak 2016-03-04 13:19:47.680355648 +0100
+++ /home/arne/.opt/pycharm-5.0.2/helpers/pycharm/django_test_manage.py 2016-03-03 13:53:49.521124476 +0100
@@ -46,6 +46,9 @@
 if not settings_file:
   settings_file = 'settings'

+import configurations
+configurations.setup()
+
 import django
 if django.VERSION[0:2] >= (1, 7):
     if not settings.configured:

Works fine for me. Of course, you need to re-apply upon re-installing or upgrading PyCharm.

Side note: if you need some early initialization typically done in manage.py, you’ll have to do it in this file as well (e.g., monkey-patching by gevent).

Managed to fix my console in a similar way. Doesn’t fix the tests, though. :\

I have the same error message in “Django Console”, in “Tools | Run manage.py Tasks” and with tests. To fix Django Console needed in Settings | Build, Execution, Deployment | Console | Django Console before if 'setup' in dir(django): django.setup() add:

os.environ.setdefault("DJANGO_CONFIGURATION", "Local")
import configurations; configurations.setup()

@Alberick0, unlike your solution I add line os.environ.setdefault("DJANGO_CONFIGURATION", "Local")

To fix Tools | “Run manage.py Tasks…” needed in the file %LocalAppData%\JetBrains\Toolbox\apps\PyCharm-P\ch-0\163.10154.50\helpers\pycharm_jb_manage_tasks_provider.py adds the same lines before django.setup()

To fix tests I modified @alonisser’s solution for working on both Linux and Windows and add necessary line `os.environ.setdefault(“DJANGO_CONFIGURATION”, “Local”)``

if sys.argv[0] and 'django_test_manage.py' in sys.argv[0]:   
  os.environ.setdefault("DJANGO_CONFIGURATION", "Local")
  import configurations   
  configurations.setup()

Too many trouble with configurations.management with pycharm. Even if I had change it, I also should tell others who in the same project. So I changed to django-environ to get a easy solution:)

@daimon99 @ivictbor You are placing the os.environ.setdefault in the wrong place, should be before if sys.argv block:

#!/usr/bin/env python
import os
import sys

os.environ.setdefault("DJANGO_SETTINGS_MODULE", "config")
os.environ.setdefault("DJANGO_CONFIGURATION", "Local")

if sys.argv[0] and sys.argv[0].find('django_test_manage.py'):
    import configurations
    configurations.setup()

if __name__ == "__main__":

    from configurations.management import execute_from_command_line

    execute_from_command_line(sys.argv)

I would strongly advice against hacking on pycharm directly, you’ll need to redo it after every update, and every teammate or future maintainer would also have to know about that