Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add dotenv read support #6407

Merged
merged 4 commits into from
May 26, 2022
Merged

Add dotenv read support #6407

merged 4 commits into from
May 26, 2022

Conversation

tannewt
Copy link
Member

@tannewt tannewt commented May 18, 2022

os.getenv() will use it (when available) to load variables from
/.env

This will also be useful when we need secrets or config for
CircuitPython outside of the VM (like WiFi credentials.)

Fixes #4212

os.getenv() will use it (when available) to load variables from
/.env

This will also be useful when we need secrets or config for
CircuitPython outside of the VM (like WiFi credentials.)

Fixes micropython#4212
@tannewt tannewt requested a review from dhalbert May 18, 2022 21:14
@tannewt
Copy link
Member Author

tannewt commented May 18, 2022

Tested with .env:

key=value
  key2 = value2
'key3' = 'value with spaces  '
# comment
key4 = value3 # comment 2
'key5'=value4
key=value5 # overrides the first one
key 6 = value 6 # won't be found
key7 = value unquoted # will go to end?
multiline = 'hello
world
how are you?'
key8 = ' escaped \\ \' '

And code.py:

import time

# time.sleep(10)

import dotenv

keys = ["wifi_ssid", "wifi_password", "foo", "key", "key1", "key2", "key3", "key4", "key5", "key 6", "key7", "key8", "multiline"]

for key in keys:
    print(key, repr(dotenv.get_key(".env", key)))

Copy link
Collaborator

@dhalbert dhalbert left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tested plain values, single-quoted values, and multi-line values, and they work for me.

But it doesn't always take the last value for multiple values:

abc=def
abc=ghi

and it gives me the first value, not the second

Adafruit CircuitPython 8.0.0-alpha.0-6-g3dca8a776 on 2022-05-23; Adafruit Metro M4 Express with samd51j19
>>> import dotenv
>>> dotenv.get_key(".env", "abc")
'def'

However, this gives the last value:

abc=def
abc=ghi
abc=jkl
>>> dotenv.get_key(".env", "abc")
'jkl'
abc=def
abc=ghi
abc=jkl
abc=mno
>>> dotenv.get_key(".env", "abc")
'jkl'

So some odd/even thing?

Copy link
Collaborator

@dhalbert dhalbert left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-tested with fix. Works great. Also works with a file without a final newline, and with \r\n linebreaks. Thanks - this is going to be be very useful!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Provide a way to set environment variables outside of code.py
2 participants