Forums

Setting environment variables, not for web apps, not for virtualenv

Hey y'all,

Fingers crossed this is an easy question. I'm trying to learn to do python properly, like maybe not setting my email username and password as a local variable! I've written a web scraper. I want it to email me when it finds a result of interest. I'm using Gmail. I'd like to set the username and password as environment variables, like so:

   gmail_login = os.environ.get('GMAIL_LOGIN')
   gmail_pw = os.environ.get('GMAIL_PW')

Where can I set environment variables in Pythonanywhere? I saw all the instructions about virtualenv, but I don't use that and don't have any good idea what it is or if I should use it. This is a 100-line practice project. Nothing online.

Thanks!

If you want them to be available in consoles, export them in your bashrc.

I tried putting this in .bashrc

export GMAIL_LOGIN = "something@gmaillll.com"
export GMAIL_PW = "password here"

But then in the script.py, this:

gmail_login = os.environ.get('GMAIL_LOGIN')
gmail_pw = os.environ.get('GMAIL_PW')

print (gmail_login)
print (gmail_pw)

Prints None None

BUT:

echo $GMAIL_LOGIN prints "something@gmaillll.com"

AND

If I run the script.py from a bash console, it prints the data I expect.

But If I just hit the "run" button on the page where I'm coding, yea, I get: None None

Hmm, that's an interesting one. When we run a script using the "run" button, then it sounds like we don't execute the .bashrc. That makes a certain amount of sense -- the script is running directly as a Python script, not under bash.

I think all I can suggest for now is that you use bash consoles if you want to run stuff that accesses your environment variables. In the long run, we need to add something where you can set environment variables that get set everywhere, in all kinds of consoles (and also scheduled tasks and web apps, which would be useful for other people even if it wouldn't be necessary here).

cool thanks

It looks like a UI for sitewide environment variables was on our to-do list, though with only one upvote so far. I've added another upvote on your behalf.

Can you add an upvote from me too? I can't access env vars from the scheduler. Took me a while to debug that.

Oh and a workaround for anyone who still wants to use environment variables in the scheduler: you can chain commands with a semicolon and instantiate the variable before the task. E.g.

export SECRET_KEY="secret"; python task.py

Upvoted -- and thanks for the tip! Another one for anyone who's struggling with this -- if you have a shell script that sets a bunch of environment variables (say, a virtualenv postactivate script) then you can chain that in too for a scheduled task using the "source" command, eg.

source /path/to/environment/script.sh && python task.py

Could you add an upvote for me, too, please.

(Unless this as been implemented in the intervening 2+ years ...)

+2

thanks- see this for the latest. we are recommending using the python-dotenv library to do this, but I will also upvote the environment variable ticket.

Add an upvote for me as well

noted