Forums

Setting environment variables " from dotenv import load_dotenv" doesn't work

Hello,

I am trying to set environment variables. I was following instructions for the following link:

https://help.pythonanywhere.com/pages/environment-variables-for-web-apps/

It doesn't seem to work on the line that says:

from dotenv import load_dotenv

It somehow works in bash. I did this to make sure it was imported properly. But it makes my site crash. Help would be very appreciated.

This is what is in my .error.log

2021-09-02 18:21:46,130: Error running WSGI application
2021-09-02 18:21:46,130: ModuleNotFoundError: No module named 'dotenv'
2021-09-02 18:21:46,130:   File "/var/www/www_ics3u_ca_wsgi.py", line 10, in <module>
2021-09-02 18:21:46,130:     from dotenv import load_dotenv
2021-09-02 18:21:46,130: ***************************************************
2021-09-02 18:21:46,131: If you're seeing an import error and don't know why,
2021-09-02 18:21:46,131: we have a dedicated help page to help you debug: 
2021-09-02 18:21:46,131: https://help.pythonanywhere.com/pages/DebuggingImportError/
2021-09-02 18:21:46,131: ***************************************************

Looks like you are running your web app with different python/venv. You need to follow https://help.pythonanywhere.com/pages/DebuggingImportError/

Thanks... didn't realize I had to pipX.Y install for the specific package... Was kinda following blindly when following instructions to pip3.6 when I should have pip3.8....

So now here a new issue poping up...

site is crashing with the following error:

RuntimeError: The session is unavailable because no secret key was set.  Set the secret_key on the application to something unique and secret.

I followed all the instructions and I don't know what could possibly be going wrong.

I have the .env file in my working directory... -assuming project directory and woring directory is the same thing... If I'm wrong... where does this .env go?

When I go to Web section of your website. itsays working directory is /home/wesamissawi I put an .env there

I also saved one in the source code directory /home/wesamisswi/mysite.

Really confused as what's happening.

Are you sure you're extracting the setting from the environment and applying it to your web framework configuration?

in my __init__.py file, I used to have

 app.config['SECRET_KEY'] = "some_key_value"

I replaced it with:

SECRET_KEY = os.getenv("SECRET_KEY")

in the .env file in my working directory, I have:

export SECRET_KEY=some_key_value

In my var/www/www_ics3u_ca_wsgi.py I added

import os
from dotenv import load_dotenv
project_folder = os.path.expanduser('/home/wesamissawi')  # adjust as appropriate
load_dotenv(os.path.join(project_folder, '.env'))

It didn't work. I also tired just adding

import os
os.environ['SECRET_KEY'] = 'some_key_value'

Also didn't work....

I believe I followed the instructions in the link provided. Any ideas?

If you replaced the app.config version with the other version, then that is probably why it is not working. You need to get the value from the environment and set it on app.config. If you have a .env file in /home/wesamissawi, then you would be loading from that .env. Does it have the SECRET_KEY setting?

Yes... the reason I'm replacing the app config line and trying to use environment variables is that I heard its more secure

Let me share a screenshot of my .env file in home/wesamissawi

this is a screenshot

I feel like I followed the instructions pretty acurately

It looks like you confused settings for different frameworks -- the bit of code you used instead of app.config['SECRET_KEY'] is meant to be used in Django configuration:

Save this change, and your code will now have access to the variable from os.getenv, so with the Django SECRET_KEY setting, you can just add this to your settings.py:

import os

SECRET_KEY = os.getenv("SECRET_KEY")

But it looks like you're using Flask, right? So, if everything else is set up correctly, you should rather use something like:

 app.config['SECRET_KEY'] = os.getenv("SECRET_KEY")

I am using Flask... That makes too much sense actually... I haven't tried it out yet but I'm sure it will work. Thanks in advance. Will update shortly.

Well isn't it beautiful when it all works out.. I missed the part where it said it was for a Django setting.

Thanks a bunch. ☺️

Glad you got that working! :-)