python-dotenv icon indicating copy to clipboard operation
python-dotenv copied to clipboard

Variable not found ("None" returned) on a Mac

Open shirki opened this issue 8 years ago • 18 comments

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!

shirki avatar Mar 28 '18 14:03 shirki

This is also something we're running into as well.

devrsw avatar Mar 29 '18 19:03 devrsw

Same as well.

Working on High Sierra, Python 3.5

TDiazT avatar Apr 01 '18 13:04 TDiazT

@theskumar Several people seem to be having this issue - would really appreciate your help on this one! Thanks in advance.

shirki avatar Apr 04 '18 08:04 shirki

@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?

theskumar avatar Apr 05 '18 18:04 theskumar

I've tried running your example on Mac, it works with:

python-dotenv==0.8.2 Python 3.6.4

theskumar avatar Apr 05 '18 18:04 theskumar

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.

garyking avatar Apr 07 '18 02:04 garyking

@theskumar Neither load_dotenv(find_dotenv()) nor providing an absolute path worked.

python-dotenv: 0.8.2 Python: 3.6.2 Mac: Sierra

shirki avatar Apr 09 '18 12:04 shirki

@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.

theskumar avatar Apr 09 '18 12:04 theskumar

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.

TDiazT avatar Apr 09 '18 15:04 TDiazT

Daaaaamn.... I just realised my file was named .env.py !! 😐

Just renamed it to .env and it worked... of course

TDiazT avatar Apr 09 '18 15:04 TDiazT

It happens no worries.

theskumar avatar Apr 09 '18 16:04 theskumar

@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!

shirki avatar Apr 23 '18 20:04 shirki

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)

joarleymoraes avatar May 02 '18 12:05 joarleymoraes

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())

ccennis avatar May 18 '18 14:05 ccennis

This solved my issue: from dotenv import load_dotenv from dotenv import find_dotenv load_dotenv(dotenv_path=find_dotenv(), verbose=True)

yashg5 avatar Sep 28 '18 06:09 yashg5

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"))

kashaziz avatar Jun 26 '20 09:06 kashaziz

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.

andhint avatar Jul 16 '20 23:07 andhint

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.

KaeganCasey avatar Nov 01 '21 16:11 KaeganCasey