Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions docs/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,18 @@ DEBUG = config('DEBUG') # lookups APP_DEBUG, returns "yes"
ENVIRONMENT = config('ENVIRONMENT') # lookups APP_ENVIRONMENT, raises KeyError as variable is not defined
```

## Custom encoding for environment files

By default, Starlette reads environment files using UTF-8 encoding.
You can specify a different encoding by setting `encoding` argument.

```python title="myproject/settings.py"
from starlette.config import Config

# Using custom encoding for .env file
config = Config(".env", encoding="latin-1")
```

## A full example

Structuring large applications can be complex. You need proper separation of
Expand Down
7 changes: 4 additions & 3 deletions starlette/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ def __init__(
env_file: str | Path | None = None,
environ: Mapping[str, str] = environ,
env_prefix: str = "",
encoding: str = "utf-8",
) -> None:
self.environ = environ
self.env_prefix = env_prefix
Expand All @@ -60,7 +61,7 @@ def __init__(
if not os.path.isfile(env_file):
warnings.warn(f"Config file '{env_file}' not found.")
else:
self.file_values = self._read_file(env_file)
self.file_values = self._read_file(env_file, encoding)

@overload
def __call__(self, key: str, *, default: None) -> str | None: ...
Expand Down Expand Up @@ -107,9 +108,9 @@ def get(
return self._perform_cast(key, default, cast)
raise KeyError(f"Config '{key}' is missing, and has no default.")

def _read_file(self, file_name: str | Path) -> dict[str, str]:
def _read_file(self, file_name: str | Path, encoding: str) -> dict[str, str]:
file_values: dict[str, str] = {}
with open(file_name) as input_file:
with open(file_name, encoding=encoding) as input_file:
for line in input_file.readlines():
line = line.strip()
if "=" in line and not line.startswith("#"):
Expand Down
7 changes: 7 additions & 0 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,3 +140,10 @@ def test_config_with_env_prefix(tmpdir: Path, monkeypatch: pytest.MonkeyPatch) -

with pytest.raises(KeyError):
config.get("ENVIRONMENT")


def test_config_with_encoding(tmpdir: Path) -> None:
path = tmpdir / ".env"
path.write_text("MESSAGE=Hello 世界\n", encoding="utf-8")
config = Config(path, encoding="utf-8")
assert config.get("MESSAGE") == "Hello 世界"