theskumar / python-dotenv

Reads key-value pairs from a .env file and can set them as environment variables. It helps in developing applications following the 12-factor principles.
https://saurabh-kumar.com/python-dotenv/
BSD 3-Clause "New" or "Revised" License
7.52k stars 424 forks source link

Doesn't accept any pathnames nor show errors on python 3.9.2 / raspberry pi #427

Open tonimelisma opened 2 years ago

tonimelisma commented 2 years ago

Either running from a script, or from the command line, it doesn't accept any pathnames as valid arguments

toni@rpi:~/temp $ pwd
/home/toni/temp
toni@rpi:~/temp $ dotenv list
ENV1=value1
ENV2=value2
toni@rpi:~/temp $ dotenv -f /home/toni/temp list
Usage: dotenv list [OPTIONS]
Try 'dotenv list --help' for help.

Error: Invalid value: Path "/home/toni/temp" does not exist.
toni@rpi:~/temp $ dotenv -f . list
Usage: dotenv list [OPTIONS]
Try 'dotenv list --help' for help.

Error: Invalid value: Path "." does not exist.
toni@rpi:~/temp $ dotenv -f / list
Usage: dotenv list [OPTIONS]
Try 'dotenv list --help' for help.

Error: Invalid value: Path "/" does not exist.
toni@rpi:~/temp $ 

In a script, load_dotenv() works if I'm in the correct directory. However, load_dotenv(dotenv_path="/home/toni/temp", verbose=True) or any other directory falls silently without populating the relevant environment variables.

The environment:

toni@rpi:~/temp $ python -V
Python 3.9.2
toni@rpi:~/temp $ cat /etc/issue
Debian GNU/Linux 11 \n \l

toni@rpi:~/temp $ uname -a
Linux rpi 5.15.61-v8+ #1579 SMP PREEMPT Fri Aug 26 11:16:44 BST 2022 aarch64 GNU/Linux
toni@rpi:~/temp $

Happy to provide further debugging information as needed. Your contribution guidelines don't provide any guidance on what's useful. Perhaps that could be something to add, too, to make issue submissions easier to look at.

bbc2 commented 1 year ago

Either running from a script, or from the command line, it doesn't accept any pathnames as valid arguments

I think dotenv list is right in refusing to work with the path of a directory, however, the error message is super confusing. I've attempted to fix this in https://github.com/theskumar/python-dotenv/pull/441. If you want you can try it before I merge it.

However, load_dotenv(dotenv_path="/home/toni/temp", verbose=True) or any other directory falls silently without populating the relevant environment variables.

I'll need more time to fix that one. The behavior of load_dotenv is not very well defined at the moment and it's not obvious what the right behavior would be.

Happy to provide further debugging information as needed. Your contribution guidelines don't provide any guidance on what's useful. Perhaps that could be something to add, too, to make issue submissions easier to look at.

Your submission is very good, thanks! It's true that some short guidelines could help.

paanvaannd commented 1 year ago

However, load_dotenv(dotenv_path="/home/toni/temp", verbose=True) or any other directory falls silently without populating the relevant environment variables.

I'm having the same issue on Python 3.10.9: load_dotenv() works perfectly fine, but providing either a relative or absolute filepath or directory path in which a .env file exists silently fails all the time, even with verbose=True.

Examples of solutions I tried:

from pathlib import Path
from dotenv import load_dotenv

dotenv_path = str(Path.cwd().absolute)
load_dotenv(dotenv_path=dotenv_path, verbose=True)
from dotenv import load_dotenv

load_dotenv(dotenv_path=".env", verbose=True)

Alas, neither approach listed here (nor any others I tried) worked. I'm happy to list them all out and provide more information if needed, and potentially try looking into contributing a PR if time/skill allow (but I don't want to make any promise I can't keep on that front).

For reference, this may be related to #104 and #171.