# main

<PyAttributes>

<PyAttribute name="StrPath" value="Union[str, 'os.PathLike[str]']">
</PyAttribute>

<PyAttribute name="logger" value="logging.getLogger(__name__)">
</PyAttribute>

</PyAttributes>

<Tabs items="[&quot;Class&quot;,&quot;Functions&quot;]">

<Tab value="Class">

<Cards>

<Card title="DotEnv" href="./DotEnv.md">
</Card>

</Cards>

</Tab>

<Tab value="Functions">

<PyFunction name="_load_dotenv_disabled" type="() -> bool">

Determine if dotenv loading has been disabled.

<PySourceCode>

```python
def _load_dotenv_disabled() -> bool:
    """
    Determine if dotenv loading has been disabled.
    """
    if "PYTHON_DOTENV_DISABLED" not in os.environ:
        return False
    value = os.environ["PYTHON_DOTENV_DISABLED"].casefold()
    return value in {"1", "true", "t", "yes", "y"}
```

</PySourceCode>

<PyFunctionReturn type="bool">
</PyFunctionReturn>

</PyFunction>

<PyFunction name="with_warn_for_invalid_lines" type="(mappings) -> Iterator[Binding]">

<PySourceCode>

```python
def with_warn_for_invalid_lines(mappings: Iterator[Binding]) -> Iterator[Binding]:
    for mapping in mappings:
        if mapping.error:
            logger.warning(
                "python-dotenv could not parse statement starting at line %s",
                mapping.original.line,
            )
        yield mapping
```

</PySourceCode>

<div>

<PyParameter name="mappings" type="Iterator[Binding]">
</PyParameter>

</div>

<PyFunctionReturn type="typing.Iterator[dotenv.parser.Binding]">
</PyFunctionReturn>

</PyFunction>

<PyFunction name="get_key" type="(dotenv_path, key_to_get, encoding='utf-8') -> Optional[str]">

Get the value of a given key from the given .env.

Returns `None` if the key isn't found or doesn't have a value.

<PySourceCode>

```python
def get_key(
    dotenv_path: StrPath,
    key_to_get: str,
    encoding: Optional[str] = "utf-8",
) -> Optional[str]:
    """
    Get the value of a given key from the given .env.

    Returns `None` if the key isn't found or doesn't have a value.
    """
    return DotEnv(dotenv_path, verbose=True, encoding=encoding).get(key_to_get)
```

</PySourceCode>

<div>

<PyParameter name="dotenv_path" type="StrPath">
</PyParameter>

<PyParameter name="key_to_get" type="str">
</PyParameter>

<PyParameter name="encoding" type="Optional[str]" value="'utf-8'">
</PyParameter>

</div>

<PyFunctionReturn type="typing.Optional[str]">
</PyFunctionReturn>

</PyFunction>

<PyFunction name="rewrite" type="(path, encoding, follow_symlinks=False) -> Iterator[Tuple[IO[str], IO[str]]]">

<PySourceCode>

```python
@contextmanager
def rewrite(
    path: StrPath,
    encoding: Optional[str],
    follow_symlinks: bool = False,
) -> Iterator[Tuple[IO[str], IO[str]]]:
    if follow_symlinks:
        path = os.path.realpath(path)

    try:
        source: IO[str] = open(path, encoding=encoding)
        try:
            path_stat = os.lstat(path)
            original_mode: Optional[int] = (
                stat.S_IMODE(path_stat.st_mode)
                if stat.S_ISREG(path_stat.st_mode)
                else None
            )
        except BaseException:
            source.close()
            raise
    except FileNotFoundError:
        source = io.StringIO("")
        original_mode = None

    with tempfile.NamedTemporaryFile(
        mode="w",
        encoding=encoding,
        delete=False,
        prefix=".tmp_",
        dir=os.path.dirname(os.path.abspath(path)),
    ) as dest:
        dest_path = pathlib.Path(dest.name)
        error = None

        try:
            with source:
                yield (source, dest)
        except BaseException as err:
            error = err

    if error is None:
        try:
            if original_mode is not None:
                os.chmod(dest_path, original_mode)

            os.replace(dest_path, path)
        except BaseException:
            dest_path.unlink(missing_ok=True)
            raise
    else:
        dest_path.unlink(missing_ok=True)
        raise error from None
```

</PySourceCode>

<div>

<PyParameter name="path" type="StrPath">
</PyParameter>

<PyParameter name="encoding" type="Optional[str]">
</PyParameter>

<PyParameter name="follow_symlinks" type="bool" value="False">
</PyParameter>

</div>

<PyFunctionReturn type="typing.Iterator[typing.Tuple[typing.IO[str], typing.IO[str]]]">
</PyFunctionReturn>

</PyFunction>

<PyFunction name="set_key" type="(dotenv_path, key_to_set, value_to_set, quote_mode='always', export=False, encoding='utf-8', follow_symlinks=False) -> Tuple[Optional[bool], str, str]">

Adds or Updates a key/value to the given .env

The target .env file is created if it doesn't exist.

This function doesn't follow symlinks by default, to avoid accidentally
modifying a file at a potentially untrusted path. If you don't need this
protection and need symlinks to be followed, use `follow_symlinks`.

<PySourceCode>

```python
def set_key(
    dotenv_path: StrPath,
    key_to_set: str,
    value_to_set: str,
    quote_mode: str = "always",
    export: bool = False,
    encoding: Optional[str] = "utf-8",
    follow_symlinks: bool = False,
) -> Tuple[Optional[bool], str, str]:
    """
    Adds or Updates a key/value to the given .env

    The target .env file is created if it doesn't exist.

    This function doesn't follow symlinks by default, to avoid accidentally
    modifying a file at a potentially untrusted path. If you don't need this
    protection and need symlinks to be followed, use `follow_symlinks`.
    """
    if quote_mode not in ("always", "auto", "never"):
        raise ValueError(f"Unknown quote_mode: {quote_mode}")

    quote = quote_mode == "always" or (
        quote_mode == "auto" and not value_to_set.isalnum()
    )

    if quote:
        value_out = "'{}'".format(value_to_set.replace("'", "\\'"))
    else:
        value_out = value_to_set
    if export:
        line_out = f"export {key_to_set}={value_out}\n"
    else:
        line_out = f"{key_to_set}={value_out}\n"

    with rewrite(dotenv_path, encoding=encoding, follow_symlinks=follow_symlinks) as (
        source,
        dest,
    ):
        replaced = False
        missing_newline = False
        for mapping in with_warn_for_invalid_lines(parse_stream(source)):
            if mapping.key == key_to_set:
                dest.write(line_out)
                replaced = True
            else:
                dest.write(mapping.original.string)
                missing_newline = not mapping.original.string.endswith("\n")
        if not replaced:
            if missing_newline:
                dest.write("\n")
            dest.write(line_out)

    return True, key_to_set, value_to_set
```

</PySourceCode>

<div>

<PyParameter name="dotenv_path" type="StrPath">
</PyParameter>

<PyParameter name="key_to_set" type="str">
</PyParameter>

<PyParameter name="value_to_set" type="str">
</PyParameter>

<PyParameter name="quote_mode" type="str" value="'always'">
</PyParameter>

<PyParameter name="export" type="bool" value="False">
</PyParameter>

<PyParameter name="encoding" type="Optional[str]" value="'utf-8'">
</PyParameter>

<PyParameter name="follow_symlinks" type="bool" value="False">
</PyParameter>

</div>

<PyFunctionReturn type="typing.Tuple[typing.Optional[bool], str, str]">
</PyFunctionReturn>

</PyFunction>

<PyFunction name="unset_key" type="(dotenv_path, key_to_unset, quote_mode='always', encoding='utf-8', follow_symlinks=False) -> Tuple[Optional[bool], str]">

Removes a given key from the given `.env` file.

If the .env path given doesn't exist, fails.
If the given key doesn't exist in the .env, fails.

This function doesn't follow symlinks by default, to avoid accidentally
modifying a file at a potentially untrusted path. If you don't need this
protection and need symlinks to be followed, use `follow_symlinks`.

<PySourceCode>

```python
def unset_key(
    dotenv_path: StrPath,
    key_to_unset: str,
    quote_mode: str = "always",
    encoding: Optional[str] = "utf-8",
    follow_symlinks: bool = False,
) -> Tuple[Optional[bool], str]:
    """
    Removes a given key from the given `.env` file.

    If the .env path given doesn't exist, fails.
    If the given key doesn't exist in the .env, fails.

    This function doesn't follow symlinks by default, to avoid accidentally
    modifying a file at a potentially untrusted path. If you don't need this
    protection and need symlinks to be followed, use `follow_symlinks`.
    """
    if not os.path.exists(dotenv_path):
        logger.warning("Can't delete from %s - it doesn't exist.", dotenv_path)
        return None, key_to_unset

    removed = False
    with rewrite(dotenv_path, encoding=encoding, follow_symlinks=follow_symlinks) as (
        source,
        dest,
    ):
        for mapping in with_warn_for_invalid_lines(parse_stream(source)):
            if mapping.key == key_to_unset:
                removed = True
            else:
                dest.write(mapping.original.string)

    if not removed:
        logger.warning(
            "Key %s not removed from %s - key doesn't exist.", key_to_unset, dotenv_path
        )
        return None, key_to_unset

    return removed, key_to_unset
```

</PySourceCode>

<div>

<PyParameter name="dotenv_path" type="StrPath">
</PyParameter>

<PyParameter name="key_to_unset" type="str">
</PyParameter>

<PyParameter name="quote_mode" type="str" value="'always'">
</PyParameter>

<PyParameter name="encoding" type="Optional[str]" value="'utf-8'">
</PyParameter>

<PyParameter name="follow_symlinks" type="bool" value="False">
</PyParameter>

</div>

<PyFunctionReturn type="typing.Tuple[typing.Optional[bool], str]">
</PyFunctionReturn>

</PyFunction>

<PyFunction name="resolve_variables" type="(values, override) -> Mapping[str, Optional[str]]">

<PySourceCode>

```python
def resolve_variables(
    values: Iterable[Tuple[str, Optional[str]]],
    override: bool,
) -> Mapping[str, Optional[str]]:
    new_values: Dict[str, Optional[str]] = {}

    for name, value in values:
        if value is None:
            result = None
        else:
            atoms = parse_variables(value)
            env: Dict[str, Optional[str]] = {}
            if override:
                env.update(os.environ)  # type: ignore
                env.update(new_values)
            else:
                env.update(new_values)
                env.update(os.environ)  # type: ignore
            result = "".join(atom.resolve(env) for atom in atoms)

        new_values[name] = result

    return new_values
```

</PySourceCode>

<div>

<PyParameter name="values" type="Iterable[Tuple[str, Optional[str]]]">
</PyParameter>

<PyParameter name="override" type="bool">
</PyParameter>

</div>

<PyFunctionReturn type="typing.Mapping[str, typing.Optional[str]]">
</PyFunctionReturn>

</PyFunction>

<PyFunction name="_walk_to_root" type="(path) -> Iterator[str]">

Yield directories starting from the given directory up to the root

<PySourceCode>

```python
def _walk_to_root(path: str) -> Iterator[str]:
    """
    Yield directories starting from the given directory up to the root
    """
    if not os.path.exists(path):
        raise IOError("Starting path not found")

    if os.path.isfile(path):
        path = os.path.dirname(path)

    last_dir = None
    current_dir = os.path.abspath(path)
    while last_dir != current_dir:
        yield current_dir
        parent_dir = os.path.abspath(os.path.join(current_dir, os.path.pardir))
        last_dir, current_dir = current_dir, parent_dir
```

</PySourceCode>

<div>

<PyParameter name="path" type="str">
</PyParameter>

</div>

<PyFunctionReturn type="typing.Iterator[str]">
</PyFunctionReturn>

</PyFunction>

<PyFunction name="find_dotenv" type="(filename='.env', raise_error_if_not_found=False, usecwd=False) -> str">

Search in increasingly higher folders for the given file

Returns path to the file if found, or an empty string otherwise

<PySourceCode>

```python
def find_dotenv(
    filename: str = ".env",
    raise_error_if_not_found: bool = False,
    usecwd: bool = False,
) -> str:
    """
    Search in increasingly higher folders for the given file

    Returns path to the file if found, or an empty string otherwise
    """

    def _is_interactive():
        """Decide whether this is running in a REPL or IPython notebook"""
        if hasattr(sys, "ps1") or hasattr(sys, "ps2"):
            return True
        try:
            main = __import__("__main__", None, None, fromlist=["__file__"])
        except ModuleNotFoundError:
            return False
        return not hasattr(main, "__file__")

    def _is_debugger():
        return sys.gettrace() is not None

    if usecwd or _is_interactive() or _is_debugger() or getattr(sys, "frozen", False):
        # Should work without __file__, e.g. in REPL or IPython notebook.
        path = os.getcwd()
    else:
        # will work for .py files
        frame = sys._getframe()
        current_file = __file__

        while frame.f_code.co_filename == current_file or not os.path.exists(
            frame.f_code.co_filename
        ):
            assert frame.f_back is not None
            frame = frame.f_back
        frame_filename = frame.f_code.co_filename
        path = os.path.dirname(os.path.abspath(frame_filename))

    for dirname in _walk_to_root(path):
        check_path = os.path.join(dirname, filename)
        if _is_file_or_fifo(check_path):
            return check_path

    if raise_error_if_not_found:
        raise IOError("File not found")

    return ""
```

</PySourceCode>

<div>

<PyParameter name="filename" type="str" value="'.env'">
</PyParameter>

<PyParameter name="raise_error_if_not_found" type="bool" value="False">
</PyParameter>

<PyParameter name="usecwd" type="bool" value="False">
</PyParameter>

</div>

<PyFunctionReturn type="str">
</PyFunctionReturn>

</PyFunction>

<PyFunction name="load_dotenv" type="(dotenv_path=None, stream=None, verbose=False, override=False, interpolate=True, encoding='utf-8') -> bool">

Parse a .env file and then load all the variables found as environment variables.

Returns:
    Bool: True if at least one environment variable is set else False

If both `dotenv_path` and `stream` are `None`, `find_dotenv()` is used to find the
.env file with it's default parameters. If you need to change the default parameters
of `find_dotenv()`, you can explicitly call `find_dotenv()` and pass the result
to this function as `dotenv_path`.

If the environment variable `PYTHON_DOTENV_DISABLED` is set to a truthy value,
.env loading is disabled.

<PySourceCode>

```python
def load_dotenv(
    dotenv_path: Optional[StrPath] = None,
    stream: Optional[IO[str]] = None,
    verbose: bool = False,
    override: bool = False,
    interpolate: bool = True,
    encoding: Optional[str] = "utf-8",
) -> bool:
    """Parse a .env file and then load all the variables found as environment variables.

    Parameters:
        dotenv_path: Absolute or relative path to .env file.
        stream: Text stream (such as `io.StringIO`) with .env content, used if
            `dotenv_path` is `None`.
        verbose: Whether to output a warning the .env file is missing.
        override: Whether to override the system environment variables with the variables
            from the `.env` file.
        encoding: Encoding to be used to read the file.
    Returns:
        Bool: True if at least one environment variable is set else False

    If both `dotenv_path` and `stream` are `None`, `find_dotenv()` is used to find the
    .env file with it's default parameters. If you need to change the default parameters
    of `find_dotenv()`, you can explicitly call `find_dotenv()` and pass the result
    to this function as `dotenv_path`.

    If the environment variable `PYTHON_DOTENV_DISABLED` is set to a truthy value,
    .env loading is disabled.
    """
    if _load_dotenv_disabled():
        logger.debug(
            "python-dotenv: .env loading disabled by PYTHON_DOTENV_DISABLED environment variable"
        )
        return False

    if dotenv_path is None and stream is None:
        dotenv_path = find_dotenv()

    dotenv = DotEnv(
        dotenv_path=dotenv_path,
        stream=stream,
        verbose=verbose,
        interpolate=interpolate,
        override=override,
        encoding=encoding,
    )
    return dotenv.set_as_environment_variables()
```

</PySourceCode>

<div>

<PyParameter name="dotenv_path" type="Optional[StrPath]" value="None">

Absolute or relative path to .env file.

</PyParameter>

<PyParameter name="stream" type="Optional[IO[str]]" value="None">

Text stream (such as `io.StringIO`) with .env content, used if
`dotenv_path` is `None`.

</PyParameter>

<PyParameter name="verbose" type="bool" value="False">

Whether to output a warning the .env file is missing.

</PyParameter>

<PyParameter name="override" type="bool" value="False">

Whether to override the system environment variables with the variables
from the `.env` file.

</PyParameter>

<PyParameter name="interpolate" type="bool" value="True">
</PyParameter>

<PyParameter name="encoding" type="Optional[str]" value="'utf-8'">

Encoding to be used to read the file.

</PyParameter>

</div>

<PyFunctionReturn type="bool">
</PyFunctionReturn>

</PyFunction>

<PyFunction name="dotenv_values" type="(dotenv_path=None, stream=None, verbose=False, interpolate=True, encoding='utf-8') -> Dict[str, Optional[str]]">

Parse a .env file and return its content as a dict.

The returned dict will have `None` values for keys without values in the .env file.
For example, `foo=bar` results in `{"foo": "bar"}` whereas `foo` alone results in
`{"foo": None}`

If both `dotenv_path` and `stream` are `None`, `find_dotenv()` is used to find the
.env file.

<PySourceCode>

```python
def dotenv_values(
    dotenv_path: Optional[StrPath] = None,
    stream: Optional[IO[str]] = None,
    verbose: bool = False,
    interpolate: bool = True,
    encoding: Optional[str] = "utf-8",
) -> Dict[str, Optional[str]]:
    """
    Parse a .env file and return its content as a dict.

    The returned dict will have `None` values for keys without values in the .env file.
    For example, `foo=bar` results in `{"foo": "bar"}` whereas `foo` alone results in
    `{"foo": None}`

    Parameters:
        dotenv_path: Absolute or relative path to the .env file.
        stream: `StringIO` object with .env content, used if `dotenv_path` is `None`.
        verbose: Whether to output a warning if the .env file is missing.
        encoding: Encoding to be used to read the file.

    If both `dotenv_path` and `stream` are `None`, `find_dotenv()` is used to find the
    .env file.
    """
    if dotenv_path is None and stream is None:
        dotenv_path = find_dotenv()

    return DotEnv(
        dotenv_path=dotenv_path,
        stream=stream,
        verbose=verbose,
        interpolate=interpolate,
        override=True,
        encoding=encoding,
    ).dict()
```

</PySourceCode>

<div>

<PyParameter name="dotenv_path" type="Optional[StrPath]" value="None">

Absolute or relative path to the .env file.

</PyParameter>

<PyParameter name="stream" type="Optional[IO[str]]" value="None">

`StringIO` object with .env content, used if `dotenv_path` is `None`.

</PyParameter>

<PyParameter name="verbose" type="bool" value="False">

Whether to output a warning if the .env file is missing.

</PyParameter>

<PyParameter name="interpolate" type="bool" value="True">
</PyParameter>

<PyParameter name="encoding" type="Optional[str]" value="'utf-8'">

Encoding to be used to read the file.

</PyParameter>

</div>

<PyFunctionReturn type="typing.Dict[str, typing.Optional[str]]">
</PyFunctionReturn>

</PyFunction>

<PyFunction name="_is_file_or_fifo" type="(path) -> bool">

Return True if `path` exists and is either a regular file or a FIFO.

<PySourceCode>

```python
def _is_file_or_fifo(path: StrPath) -> bool:
    """
    Return True if `path` exists and is either a regular file or a FIFO.
    """
    if os.path.isfile(path):
        return True

    try:
        st = os.stat(path)
    except (FileNotFoundError, OSError):
        return False

    return stat.S_ISFIFO(st.st_mode)
```

</PySourceCode>

<div>

<PyParameter name="path" type="StrPath">
</PyParameter>

</div>

<PyFunctionReturn type="bool">
</PyFunctionReturn>

</PyFunction>

</Tab>

</Tabs>
