Tuesday, February 6, 2024

[SOLVED] Environment Variable not loading with load_dotenv() in Linux

Issue

I'm trying to make a discord bot, and when I try to load a .env with load_dotenv() it doesn't work because it says

Traceback (most recent call last):
  File "/home/fanjin/Documents/Python Projects/Discord Bot/bot.py", line 15, in <module>
    client.run(TOKEN)
  File "/home/fanjin/.local/lib/python3.8/site-packages/discord/client.py", line 708, in run
    return future.result()
  File "/home/fanjin/.local/lib/python3.8/site-packages/discord/client.py", line 687, in runner
    await self.start(*args, **kwargs)
  File "/home/fanjin/.local/lib/python3.8/site-packages/discord/client.py", line 650, in start
    await self.login(*args, bot=bot)
  File "/home/fanjin/.local/lib/python3.8/site-packages/discord/client.py", line 499, in login
    await self.http.static_login(token.strip(), bot=bot)
AttributeError: 'NoneType' object has no attribute 'strip

Here's my code for the bot:

import os

import discord
from dotenv import load_dotenv

load_dotenv()
TOKEN = os.getenv('DISCORD_TOKEN')

client = discord.Client()

@client.event
async def on_ready():
    print(f'{client.user} has connected to Discord!')

client.run(TOKEN)

And the save.env file: (It's a fake token)

# .env
DISCORD_TOKEN={XXXXXXXX}

Both files are in the same directory, and I even tried to explicitly specify the .env's path with

env_path = Path('path/to/file') / '.env'
load_dotenv(dotenv_path=env_path)

but that also didn't work


Solution

You need to put the full path.

Use

  • either os.path.expanduser('~/Documents/MY_PROJECT/.env')

  • or: load_dotenv('/home/MY_USER/Documents/MY_PROJECT/.env')

and it will work.

Or you change your current working directory in your code editor to where the ".env" file is (which should be the project folder).

Or you open the project folder in the menu of your code editor, this should make the project folder the current working directory.

On Linux, you can also go to the project folder in the terminal and start the code editor from there, type for example codium or whatever you use in the command prompt.

TL:DR

Quote from the other answer

Since, I moved my .env file's inside another subfolder config, then I had to provide the full path to load_dotenv() to make it work.

This gave me the idea of checking the working directory.

Current working directory

os.getcwd() gave me a folder further up the tree. And then I copied the ".env" file into that working directory and it worked.

Changing the working directory depends on your code editor. I use codium, which is the open source version of vscode, then you may follow for example Python in VSCode: Set working directory to python file's path everytime

Full path

You can also put the full path.

Funny enough, I had checked that before coming here, but I copied the path that you get from the terminal, starting with '~/Documents/MY_PROJECT, which does not find the file but does not alert either, any tried environment variables were just empty - just because the ".env" file itself was never read.



Answered By - questionto42
Answer Checked By - David Goodson (WPSolving Volunteer)