Skip to content

Conversation

secrett2633
Copy link

Summary

Fixes UnicodeDecodeError by allowing explicit encoding selection when reading .env files.

Resolves #2016

Description

Modified the _read_file method in starlette/config.py to accept an encoding parameter.
This ensures .env files can be correctly decoded regardless of the system's default locale encoding.

Key changes:

  • Added encoding parameter (default: "utf-8") to _read_file
  • Passed encoding explicitly to open() when reading .env files
  • Retained existing parsing logic (key/value handling unchanged)

Motivation

The current implementation uses open(file_name) without specifying an encoding, which defaults to the system’s locale (e.g., GBK on Windows in Chinese locales).
This leads to UnicodeDecodeError when the .env file is encoded in UTF-8 (a common format).

Expected Outcome

After this fix:

  • ✅ UTF-8 .env files can be read on Windows systems using GBK/CP949 locales
  • ✅ Developers have flexibility to override encoding if needed
  • ✅ Backward compatibility preserved (utf-8 is default)
  • ✅ No additional runtime cost introduced

Checklist

  • I understand that this PR may be closed in case there was no previous discussion. (This doesn't apply to typos!)
  • I've added a test for each change that was introduced, and I tried as much as possible to make a single atomic change.
  • I've updated the documentation accordingly.

@Kludex
Copy link
Owner

Kludex commented Sep 6, 2025

Hi @secrett2633 ,

Thanks for the sponsorship! :)

Copy link
Owner

@Kludex Kludex left a comment

Choose a reason for hiding this comment

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

This doesn't seem all that is needed.

I guess we need to pass encoding from the Config class, otherwise this can't ever be different.

Although, I'm inclined to suggest users to use PYTHONUTF8=1 on their environment.

@secrett2633
Copy link
Author

secrett2633 commented Sep 6, 2025

Thanks! Happy to help. 😊 @Kludex

You're right that without passing encoding from the Config class it wouldn't be configurable.

I've adjusted the implementation based on that feedback.

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.

2 participants