Code Monkey home page Code Monkey logo

django-dotenv's Introduction

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:

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

import 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:

import os

import dotenv
from django.core.wsgi import get_wsgi_application

dotenv.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).

django-dotenv's People

Contributors

alexdlaird avatar halfdan avatar inglesp avatar jacobian avatar jamieconnolly avatar jpadilla avatar kevgathuku avatar merwok avatar mfonism avatar pupeno avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

django-dotenv's Issues

Use default value if variable not defined in .env

Is there a way to pass an optional value to use if the variable does not exist in the .env file?

For example, say I have the following content in .env file

DEBUG=False

And in settings.py file, I have

DEBUG=os.environ.get('DEBUG')

I want to set DEBUG in settings.py file to False if the variable is not defined in the .env file.

Is it possible to just load dotenv in the settings?

Loading dotenv in manage.py/wsgi.py/etc is a bit cumbersome, because I've just spent ten minutes trying to debug why my Celery won't load it. Can I just load it at the top of my settings.py, os there something wrong with that? If not, can the docs be changed to reflect this?

Why?

I looked at the source code for this project -- none of it appears to be django specific.

Why is it branded with django?

Breaks python-dotenv

This package interferes with python-dotenv.

if you have a project that uses python-dotenv, but have already installed this it will break.

This is because this project uses "dotenv.py" in the root folder of site-packages, but the other tool uses a package.

Suggestion: move the file into it's own package, e.g. django_dotenv and consider renaming it.

Allow preventing or suppressing UserWarning

We use dotenv on our project/wsgi.py and project/celery.py for local development where we do have a .env. When we deploy to Heroku our logs get polluted with UserWarnings.

Workaround is to do the following:

with warnings.catch_warnings():
    warnings.simplefilter('ignore')
    dotenv.read_dotenv()

Import error with coverage

I have a file tmp.py that imports dotenv, and I've pip-installed django-dotenv==1.3.0 and coverage==3.7.1. When I run the file as a script, it works just fine. But when I run it with coverage, an ImportError is raised:

$ coverage run tmp.py 
Traceback (most recent call last):
  File "tmp.py", line 1, in <module>
    import dotenv
ImportError: No module named dotenv

FYI I discovered this when running coverage run manage.py test using this Django template. Any thoughts?

pipenv install produces ERROR: Could not find a version that matches django-dotenv

System details:
Ubuntu - 16.04
Python - 3.7
Pip - 9.0.1
Pipenv - 2018.11.26

This is my Pipfile

[[source]]
url = "https://pypi.org/simple"
name = "pypi"

[packages]
django-dotenv = "*"

[requires]
python_version = "3.7"

When I run pipenv install I get pipenv.exceptions.ResolutionFailure: ERROR: ERROR: Could not find a version that matches django-dotenv

Is it possible that djang-dotenv is not visible to pypi.org/simple?

Add "safe" option which requires .env.example

Would you be open to the idea of adding a safe=False option to read_dotenv? The way it would work is similar to dotenv-safe for Node.js, i.e. require the existence of .env.example and make sure that each entry from the example contains a value in .env.

Is there a reason not to override variables

I noticed you are using os.environ.setdefault(k, v) which means if there is an already populated key in the current environment it will be ignored. Was this intended?

Thanks

Multiline support

Heroku supports multiline strings for environment values, useful for storing RSA private key values instead of adding it to the repository. Currently django-dotenv will only add the first line found for that value. Ideally it would find a value with an opening double quote and read until it find the next one.

easily allow sourcing .env-file in bash

django-dotenv is a python-library, and used in python-code.
Currently .env-files can only contain:

  • key/value-pairs
  • blank lines
  • comments

But sometimes, env-vars are also used by executables outside your python-code.
E.g In a django-project with a postgresql-backend, I have these env-vars:

# postgres - used in django-settings + by postgres-executables (bin/pg_* )
PGHOSTADDR="127.0.0.1"
PGPORT="5432"
PGDATABASE="my-db"
PGUSER="my-dbadmin"
PGPASSWORD="SUPERSECRET"
PGOPTIONS=""

# used only by postgres-executables, 
# cfr. https://www.postgresql.org/docs/12.1/libpq-envars.html
PGDATA="..."
PGHOST="..."

I use following snippet to load all these key/value-pairs in my current bash-session
to use the postgresql-commands:
`

set -a && source .env && set +a
`

You can find many oneliners to source a .env-file in bash, many using grep/sed/xargs/...

If we would allow setting bash-options in the .env-file:

set -o allexport
PGHOSTADDR="127.0.0.1"
...
set -o allexport

these option-lines can just be skipped in the python-parsing of the dotenv

this would simplify to:

 > source .env

just source the .env!

Readme WSGI example doesn't find the correct folder

Hello,

Trying to run the WSGI example on readme doesn't work when runned in any other folder than the one the .env lives.

For example, having a folder with a .env a file named main.py:

import os
import dotenv

print("Loading file:", os.path.join(os.path.dirname(os.path.dirname(__file__)), '.env'))
dotenv.read_dotenv(os.path.join(os.path.dirname(os.path.dirname(__file__)), '.env'))

Running on the same folder works without problems:

$ python main.py
Loading file: .env

Moving to another folder, fails:

$ python path/to/main.py
Loading file: .env
dot_env_example/main.py:5: UserWarning: Not reading .env - it doesn't exist.
  dotenv.read_dotenv(os.path.join(os.path.dirname(os.path.dirname(__file__)), '.env'))

This can be avoided removing the duplicated os.path.dirname part:

 $ python path/to/main.py
Loading file: path/to/.env

Is the parameter supposed to be entered relative to the main.py folder, or relative to the folder being run?

Cheers!

Allow (or enforce) encryption of .env file

Hi - thanks for building this great repo!

I noticed that you suggest adding .env to the .gitignore file - presumably because the config shouldn't be exposed as part of the repository?

Do you have a suggestion on how to share the .env file with members of my team?

The best answer I could find suggests checking in the .env file and encrypting it as an option.

What do maintainers think about allowing encryption of the .env file with one master password? I'd be happy to help with a PR if it would be welcomed and someone could help me think through best practice implementation.

Or, is there a better way to share the .env file with my team?

environment variables are not working on server

I have following directory structure of the Django application

/var/www/html/project/
  - src
    |- project
       |- settings.py
    |- manage.py
    |- .env

and content of .env file

DEBUG=True

and in settings file, I have

DEBUG = eval(os.environ.get('DEBUG', str(False)))

But it never loads from the .env file and DEBUG is always set to False

When I checked the same in Django shell, it was working there

$ python manage.py shell

$ import os
$ os.environ.get('DEBUG')
# True

What could be the reason that same is working in the shell but not in the settings file?

Usage with Django and Celery

I struggle a lot to load environments variables in from settings in celery.py file when working with both Django and celery.

since I was loading env from manage.py I had issued to load use them in a celery.py file.
I had to load them twice, in manage.py and celery.py.

What is the problem to load the environment directly in the settings.py file?
If there is no problem with that approach, we can update the readme by saying that we can load them in settings.py file, not in manage.py

PS: If this doesn't look clear, I will come back to update the description.

seems broken in python3

In python 3.6 the .env doesn't seem to be read and applied to the environment as it was in python 2.7. No variables set within it appear.

Use dotenv with Gunicorn

When I put the

import dotenv
dotenv.read_dotenv()

on manage.py it runs ok, but when I wan to use Gunicorn to production and put it in wsgi.py, it did not read the .env file. Any idea?

Can't use with pytest-django and pytest-dotenv

Hi! I am using pytest-dotenv, but it installs python-doenv by default and can't work without it. So, I have error when run django AttributeError: module 'dotenv' has no attribute 'read_dotenv'. Did you mean: 'load_dotenv'?. When run tests, all is fine.

What could you offer to prevent it?

dotenv use in django script

I am running script in django. Can any one suggest how to pass dot env in that script.

dotenv.read_dotenv(os.path.join(os.path.dirname(os.path.dirname(__file__)), 'local.env'))
 os.environ.setdefault("DJANGO_SETTINGS_MODULE", "sample_project.settings")
 import django
django.setup()

i am not able to get setting in this case. Can anyone suggest how to achieve this using dotenv.?

Deprecation warning due to invalid escape sequences

Deprecation warning due to invalid escape sequences. Using raw strings or escaping them again helps in resolving this. Check https://github.com/asottile/pyupgrade/ for automatic fix of this.

find . -iname '*.py' | grep -Ev 'setup|rdf4|tool' | xargs -P4 -I{} python3.8 -Wall -m py_compile {}
./tests.py:65: DeprecationWarning: invalid escape sequence \$
  env = parse_dotenv('FOO="foo\${BAR}"')

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.