question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Problems with Cookiecutter + Docker + PyCharm

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Reactions:1
  • Comments:18 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
foarsittercommented, Jun 6, 2019

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.

2reactions
floer32commented, Apr 18, 2019

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 😬

Read more comments on GitHub >

github_iconTop Results From Across the Web

Problems with Cookiecutter + Docker + PyCharm · Issue #1766
Hello everyone! I have some issues getting comfy with Cookiecutter, Docker and PyCharm. I'm using current commit ( d95bdc6 ) with latest ...
Read more >
Docker Compose problems – IDEs Support (IntelliJ Platform)
I'm trying to setup my first Django project using PyCharm and Docker and I've run into some problems. I'm using the cookiecutter-django ...
Read more >
Entrypoint is ignored on running Python scripts using Docker ...
Relates to 2 issues (1 unresolved). Cookiecutter-django project doesn't work with docker-compose ... I'm trying to use docker-compose with Django.
Read more >
Python console not working with remote docker interpreter
We are looking for the solution now to get PyCharm Python console and debugger for Docker interpreters independent from network setup.
Read more >
Multiple images with docker compose : PY-17983 - YouTrack
We would like to add full PyCharm and Docker support to cookiecutter-django project. Implementation based on dockerized ssh service was prepared, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found