cookiecutter-django: Problems with Cookiecutter + Docker + PyCharm

Hello everyone!

I have some issues getting comfy with Cookiecutter, Docker and PyCharm.

I’m using current commit ( https://github.com/pydanny/cookiecutter-django/commit/d95bdc6a378062382c536da07364bf8c46f8ea9c ) with latest PyCharm Professional (2018.2.2) and latest docker-ce (18.06.1 ce) and latest docker-compose (1.22.0) on Ubuntu 18.04.1 LTS.

One of my biggest problems is, that the manage.py shell doesn’t work as it used to in earlier versions of Cookiecutter. Things like createsuperuser and shell worked flawlessly before.

Now I get errors trying to do this. For example this appears when trying to call on the manage.py shell via CTRL+ALT+R (calling manage.py) and running shell:

docker-compose -f ...PROJECT/local.yml -f .../.PyCharm2018.2/system/tmp/docker-compose.override.362.yml run --rm -p 0.0.0.0:33593:33593 django
Starting janustats_mailhog_1 ... 
Starting janustats_pycharm_helpers_1 ... 
Starting janustats_mailhog_1         ... done
Starting janustats_postgres_1        ... 
Starting janustats_postgres_1        ... done
Starting janustats_pycharm_helpers_1 ... done
import sys; print('Python %s on %s' % (sys.version, sys.platform))
import django; print('Django %s' % django.get_version())
sys.path.extend(['/opt/project', '/opt/.pycharm_helpers/pycharm', '/opt/.pycharm_helpers/pydev'])
if 'setup' in dir(django): django.setup()
import django_manage_shell; django_manage_shell.run("/home/.../.../PROJECT")
Python 3.6.6 (default, Aug 22 2018, 20:48:31) 
Type 'copyright', 'credits' or 'license' for more information
IPython 6.5.0 -- An enhanced Interactive Python. Type '?' for help.
PyDev console: using IPython 6.5.0
Python 3.6.6 (default, Aug 22 2018, 20:48:31) 
[GCC 6.4.0] on linux
Django 2.0.8
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/environ/environ.py", line 273, in get_value
    value = self.ENVIRON[var]
  File "/usr/local/lib/python3.6/os.py", line 669, in __getitem__
    raise KeyError(key) from None
KeyError: 'DATABASE_URL'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/IPython/core/interactiveshell.py", line 2961, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-1-2c10784a87dc>", line 6, in <module>
    if 'setup' in dir(django): django.setup()
  File "/usr/local/lib/python3.6/site-packages/django/__init__.py", line 19, in setup
    configure_logging(settings.LOGGING_CONFIG, settings.LOGGING)
  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 43, in _setup
    self._wrapped = Settings(settings_module)
  File "/usr/local/lib/python3.6/site-packages/django/conf/__init__.py", line 106, in __init__
    mod = importlib.import_module(self.SETTINGS_MODULE)
  File "/usr/local/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 955, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 665, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 678, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "/opt/project/config/settings/local.py", line 1, in <module>
    from .base import *  # noqa
  File "/opt/project/config/settings/base.py", line 41, in <module>
    'default': env.db('DATABASE_URL'),
  File "/usr/local/lib/python3.6/site-packages/environ/environ.py", line 204, in db_url
    return self.db_url_config(self.get_value(var, default=default), engine=engine)
  File "/usr/local/lib/python3.6/site-packages/environ/environ.py", line 277, in get_value
    raise ImproperlyConfigured(error_msg)
django.core.exceptions.ImproperlyConfigured: Set the DATABASE_URL environment variable

Running migrate and runserver from the predefined run/debug configs works. Copying the migrate config and changing “migrate” to ‘createsuperuser’ or ‘shell’ doesn’t work. (No debug information to be seen, just an either exiting or seemingly freezing container.)

I’ve checked the pycharm configuration.rst but couldn’t find hints to my problem there.

Please help me to find out how to properly call upon the manage.py shell and how to createsuperuser within PyCharm using docker-compose again. Sorry in advance if I missed something in the documentation regarding my problem.

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Reactions: 1
  • Comments: 18 (1 by maintainers)

Commits related to this issue

Most upvoted comments

The compose/production/django/entrypoint scripts adds the DATABASE_URL and CELERY_BROKER_URL env variables. Pycharm does not use the entrypoint script and therefore the variables are missing.

Add the next code after the import statements in File | Settings | Build, Execution, Deployment | Console | Django Console fixes the issue.

import os
os.environ.setdefault("DATABASE_URL","postgres://{}:{}@{}:{}/{}".format(
    os.environ['POSTGRES_USER'], 
    os.environ['POSTGRES_PASSWORD'], 
    os.environ['POSTGRES_HOST'], 
    os.environ['POSTGRES_PORT'], 
    os.environ['POSTGRES_DB']
))
os.environ.setdefault("CELERY_BROKER_URL", os.environ['REDIS_URL'])

Pycharm stores this setting in .idea/workspace.xml. Adding this file will solve the issue I hope.

I love PyCharm - but why not close this issue. It’s generally a better practice to use a dedicated terminal emulator (I’m partial to Kitty). You’re stunting your productivity if you’re staying in an IDE-embedded shell 😬

Settings -> Project Interpreter -> Add -> Docker-compose -> Choose the local.yml compose file

I believe this is available only for the pro version, not the community version.