Forums

PYTHONANYWHERE_DOMAIN en var is not defined in ssh session

Hi,

Even though PYTHONANYWHERE_DOMAIN is defined in bash console, or python console and environment, it is not defined in an ssh session.

I am relying on that to detect if my app is running in production or in dev/debug.

I have inserted that snippet in .bashrc

if [ -n "$SSH_CLIENT" ] ; then export PYTHONANYWHERE_DOMAIN="eu.pythonanywhere.com" fi

Is this good ?

Is there a better way to detect if I am running the app on pythonanywhere ?

That looks fine to me. If you want to, you could set a specific environment variable in your .bashrc that you can use to detect running on PythonAnywhere but, if what you have is working, then just stick with it.

OK. Yes I had tried at one point in time to use a custom env var and I don't why I reverted to this solution.

Thanks

I want to emphasize that there is a difference in the environment variables defined/available in ssh session and in a pythonanywhere bash console.

Like said above, PYTHNOANYWHERE_DOMAIN is not defined in ssh session Also, once you have enabled the API and get an API token, API_TOKEN is also not set in ssh session.

Is there a security reason for not settings those variable there, while there are defined elsewhere ? Right now, I don't see why, they could not be set.

May be there is another reason ?

There is this too: https://help.pythonanywhere.com/pages/environment-variables-for-web-apps

You're right, it looks like we're not setting some environment variables in SSH sessions. That is a bug, and is something that we can only fix by moving our SSH system over to using our new virtualization system (which is currently used by everything apart from SSH and Jupyter notebooks -- and notebooks get the environment variables via another route).

Unfortunately that's going to take a while to fix -- the reason SSH has not been moved over is because it's going to be tricky. I reckon that the best short-term solution for your underlying issue would be to put something in your .bashrc on PythonAnywhere -- say, an explicit

export ON_PYTHONANYWHERE=true

...and rely on that for now.