Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/jpadilla/django-dotenv
Loads environment variables from .env
https://github.com/jpadilla/django-dotenv
django dotenv python
Last synced: about 1 hour ago
JSON representation
Loads environment variables from .env
- Host: GitHub
- URL: https://github.com/jpadilla/django-dotenv
- Owner: jpadilla
- License: mit
- Created: 2013-06-13T15:32:20.000Z (over 11 years ago)
- Default Branch: master
- Last Pushed: 2021-11-29T15:55:42.000Z (about 3 years ago)
- Last Synced: 2025-01-04T11:40:07.310Z (7 days ago)
- Topics: django, dotenv, python
- Language: Python
- Homepage:
- Size: 28.3 KB
- Stars: 570
- Watchers: 10
- Forks: 42
- Open Issues: 17
-
Metadata Files:
- Readme: README.rst
- License: LICENSE
Awesome Lists containing this project
- starred-awesome - django-dotenv - Loads environment variables from .env (Python)
README
django-dotenv
=============|build-status-image| |pypi-version|
`foreman `__ reads from ``.env``.
``manage.py`` doesn't. Let's fix that.Original implementation was written by
`@jacobian `__.Tested on Python 3.5, 3.6, 3.7 and 3.8.
Installation
------------::
pip install django-dotenv
Usage
-----Your ``manage.py`` should look like:
.. code:: python
#!/usr/bin/env python
import os
import sysimport dotenv
if __name__ == "__main__":
dotenv.read_dotenv()os.environ.setdefault("DJANGO_SETTINGS_MODULE", "project.settings")
try:
from django.core.management import execute_from_command_line
except ImportError as exc:
raise ImportError(
"Couldn't import Django. Are you sure it's installed and "
"available on your PYTHONPATH environment variable? Did you "
"forget to activate a virtual environment?"
) from exc
execute_from_command_line(sys.argv)You can also pass ``read_dotenv()`` an explicit path to the ``.env``
file, or to the directory where it lives. It's smart, it'll figure it
out.By default, variables that are already defined in the environment take
precedence over those in your ``.env`` file. To change this, call
``read_dotenv(override=True)``.Check out
`tests.py `__
to see all the supported formats that your ``.env`` can have.Using with WSGI
~~~~~~~~~~~~~~~If you're running Django with WSGI and want to load a ``.env`` file,
your ``wsgi.py`` would look like this:.. code:: python
import os
import dotenv
from django.core.wsgi import get_wsgi_applicationdotenv.read_dotenv(os.path.join(os.path.dirname(os.path.dirname(__file__)), '.env'))
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "project.settings")
application = get_wsgi_application()
That's it. Now go 12 factor the crap out of something.
Common problems
---------------``AttributeError: module 'dotenv' has no attribute 'read_dotenv'``
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~There is another similar package,
`python-dotenv `__, which also
contains a module called ``dotenv``. If that package is installed, then you
will see:::
AttributeError: module 'dotenv' has no attribute 'read_dotenv'
To resolve this, uninstall python-dotenv.
``read_dotenv`` is not reading from my environment file!
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~By default, variables that are already defined in the environment take
precedence over those in your ``.env`` file. To change this, call
``read_dotenv(override=True)``... |build-status-image| image:: https://travis-ci.org/jpadilla/django-dotenv.svg
:target: https://travis-ci.org/jpadilla/django-dotenv
.. |pypi-version| image:: https://img.shields.io/pypi/v/django-dotenv.svg
:target: https://pypi.python.org/pypi/django-dotenv