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.

The DATABASE_URL environment variable couldn't found in shell

See original GitHub issue

What happened?

/app # python manage.py shell_plus

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 “manage.py”, line 30, in <module> execute_from_command_line(sys.argv) File “/usr/local/lib/python3.6/site-packages/django/core/management/init.py”, line 371, in execute_from_command_line utility.execute() File “/usr/local/lib/python3.6/site-packages/django/core/management/init.py”, line 365, in execute self.fetch_command(subcommand).run_from_argv(self.argv) File “/usr/local/lib/python3.6/site-packages/django/core/management/init.py”, line 204, in fetch_command 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 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 “/app/config/settings/local.py”, line 1, in <module> from .base import * # noqa File “/app/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

What should’ve happened instead?

I found the below command in ./compose/production/django/entrypoint that “export DATABASE_URL” is only valid for the current shell, when you start a new shell, there is no DATABASE_URL environment variable. export DATABASE_URL="postgres://${POSTGRES_USER}:${POSTGRES_PASSWORD}@${POSTGRES_HOST}:${POSTGRES_PORT}/${POSTGRES_DB}"

why not place this variable in ./.env/.local/.django? It always seems to be valid in the container.

Steps to reproduce

I started a project powered by latest cookiecutter-django version using docker-compose, and when I executed “docker-compose -f local.yml up -d”, the app had been successful launched, and the http://0.0.0.0:8000/ can be visited successfully. But when I executed “docker exec -it bila_django_1(my container name) sh”, and when I want to do anything like “python manage.py shell_plus”, there is a error like above.

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:8

github_iconTop GitHub Comments

11reactions
simonbogardecommented, Sep 19, 2019

@zmrenwu I just had the same problem and what works for me is to source the entrypoint file so that the env variables become available: Within container shell: source compose/production/django/entrypoint

9reactions
elcoliecommented, Apr 28, 2020

After you are in django container run source /entrypoint

Read more comments on GitHub >

github_iconTop Results From Across the Web

The DATABASE_URL environment variable couldn't found in ...
I found the below command in ./compose/production/django/entrypoint that "export DATABASE_URL" is only valid for the current shell, when you ...
Read more >
Set the DATABASE_URL environment variable - Stack Overflow
Sourcing the file with the environmental variables and then running the python script from the same shell should be sufficient. source .
Read more >
Working with Environment Variables in Python - Twilio
Using this method, if you try to import an environment variable that doesn't exist Python will raise a KeyError exception: >>> database_url ...
Read more >
Environment variables - Prisma
An environment variable is a key value pair of string data that is stored on your machine's local environment. Refer to our Environment...
Read more >
Heroku Postgres
You can track its status with the heroku pg:wait command, ... you must export the DATABASE_URL environment variable for your app to connect ......
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