Variable not found ("None" returned) on a Mac
Hi, we are using dotenv for a project and are getting strange behavior.
While the environment variable is working just fine on a Windows machine, it is not able to read at all on a Mac. It returns "None" even though the variable is clearly there. There seem to be no other issues.
In X.py file:
from dotenv import load_dotenv
load_dotenv()
import os
HOME_PATH = os.getenv("SQUABBLE_HOME_PATH")
In .env file:
SQUABBLE_HOME_PATH = '/path/to/squabble_runner/'
While running:
Traceback (most recent call last):
File "XXXX/X.py", line 27, in controversy_score
input_file_path = HOME_PATH + 'lib/squabble_input.txt'
TypeError: unsupported operand type(s) for +: 'NoneType' and 'str'
This is on Sierra, Python 3.6.2.
As mentioned above, the same code runs just fine on a PC. Any help would be appreciated!
This is also something we're running into as well.
Same as well.
Working on High Sierra, Python 3.5
@theskumar Several people seem to be having this issue - would really appreciate your help on this one! Thanks in advance.
@shirki can you try providing absolute path to .env file or use find_dotenv() in the call to load_dotenv() see if you are still facing issue?
I've tried running your example on Mac, it works with:
python-dotenv==0.8.2 Python 3.6.4
I have the same problem as others here. If I use load_dotenv() then it doesn't work. If I use load_dotenv(find_dotenv()) then it works fine though.
@theskumar Neither load_dotenv(find_dotenv()) nor providing an absolute path worked.
python-dotenv: 0.8.2 Python: 3.6.2 Mac: Sierra
@shirki sorry for the issues you are facing. But I don't seem to able to reproduce the issue. One last time, I would just advice you to use the absolute path to the .env file. It could be file permission issue, but I can't say for sure.
I tried running load_dotenv(verbose=True) and I get:
UserWarning: File doesn't exist
warnings.warn("File doesn't exist {}".format(self.dotenv_path))
I tried the other example in the doc:
from pathlib import Path # python3 only
env_path = Path('.') / '.env'
load_dotenv(dotenv_path=env_path)
which results in
TypeError: argument should be string, bytes or integer, not PosixPath
I str(env_path) to which I still get that the .env file doesn't exist.
I also tried:
from dotenv import load_dotenv
import os
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
env_path = os.path.join(BASE_DIR, '.env')
load_dotenv(dotenv_path=env_path, verbose=True)
but still the same error.
Daaaaamn.... I just realised my file was named .env.py !! 😐
Just renamed it to .env and it worked... of course
It happens no worries.
@theskumar Can you please reopen the issue? I tested it again with python 3.6.5, as well as with an absolute filename or with the load function, and the issue persists. As opposed to @TDiazT, my file name is definitely .env 😄. No file permission issues either.
Let me know what you need from me in order to reproduce the issue. As I mentioned, this is particularly with one mac and one PC on the same team.
@devrsw, are you still getting the same error?
Thanks!
It happened to me as well on Docker/Ubuntu14.04. I had to explicitly load with dotenv_path:
from dotenv import load_dotenv
load_dotenv(dotenv_path='.env', verbose=True)
i'm on a mac, high sierra and I had the same issue until I changed it to:
from dotenv import load_dotenv
from dotenv import find_dotenv
load_dotenv(find_dotenv())
This solved my issue: from dotenv import load_dotenv from dotenv import find_dotenv load_dotenv(dotenv_path=find_dotenv(), verbose=True)
I had similar issue on Python 3.5 (working fine on later versions). What worked for me is using explicit path to .env. Assuming the env file is in projects folder, it can be loaded as follows:
load_dotenv(os.path.join(BASE_DIR, "backend", ".env"))
I had similar issues where adding the path or find_dotenv() didn't work. I recreated my .env file to be sure it had the right extension and that fixed it. I'm guessing I messed something up creating the .env the first time somehow.
Saw this issue and wanted to post what worked for me since none of the above fixes were working for my problem.
I had trouble getting python-dotenv to work because I had jupyterlab installed in my conda (miniforge) base environment and had installed python-dotenv in a separate environment. python-dotenv wasn't recognizing the .env file correctly from jupyterlab and after a lot of trouble shooting all I had to do was install python-dotenv in my base conda env as well. Then load_dotenv() correctly recognized my .env file in the same folder as my notebook.
Just wanted to post in case someone else had the same problem.