How can I detect Heroku's environment?
Solution 1
An ENV var seems to the most obvious way of doing this. Either look for an ENV var that you know exists, or set your own:
on_heroku = False
if 'YOUR_ENV_VAR' in os.environ:
on_heroku = True
more at: http://devcenter.heroku.com/articles/config-vars
Solution 2
Similar to what Neil suggested, I would do the following:
debug = True
if 'SOME_ENV_VAR' in os.environ:
debug = False
I've seen some people use if 'PORT' in os.environ:
But the unfortunate thing is that the PORT variable is present when you run foreman start
locally, so there is no way to distinguish between local testing with foreman and deployment on Heroku.
I'd also recommend using one of the env vars that:
- Heroku has out of the box (rather than setting and checking for your own)
- is unlikely to be found in your local environment
At the date of posting, Heroku has the following environ variables:
['PATH', 'PS1', 'COLUMNS', 'TERM', 'PORT', 'LINES', 'LANG', 'SHLVL', 'LIBRARY_PATH', 'PWD', 'LD_LIBRARY_PATH', 'PYTHONPATH', 'DYNO', 'PYTHONHASHSEED', 'PYTHONUNBUFFERED', 'PYTHONHOME', 'HOME', '_']
I generally go with if 'DYNO' in os.environ:
, because it seems to be the most Heroku specific (who else would use the term dyno, right?).
And I also prefer to format it like an if-else statement because it's more explicit:
if 'DYNO' in os.environ:
debug = False
else:
debug = True
Solution 3
First set the environment variable ON_HEROKU
on heroku:
$ heroku config:set ON_HEROKU=1
Then in settings.py
import os
# define if on heroku environment
ON_HEROKU = 'ON_HEROKU' in os.environ
Solution 4
Read more about it here: https://devcenter.heroku.com/articles/config-vars
My solution:
$ heroku config:set HEROKU=1
These environment variables are persistent – they will remain in place across deploys and app restarts – so unless you need to change values, you only need to set them once.
Then you can test its presence in your app.:
>>> 'HEROKU' in os.environ
True
Solution 5
The most reliable way would be to set an environment variable as above. If that's not possible, there are a few signs you can look for in the filesystem, but they may not be / are not foolproof
Heroku instances all have the path
/app
- the files and scripts that are running will be under this too, so you can check for the presence of the directory and/or that the scripts are being run from under it.There is an empty directory
/etc/heroku
/etc/hosts
may have some heroku related domains added~ $ cat /etc/hosts <snip>.dyno.rt.heroku.com
Any of these can and may change at any moment.
Your milage may vary
aviraldg
Updated on June 03, 2022Comments
-
aviraldg almost 2 years
I have a Django webapp, and I'd like to check if it's running on the Heroku stack (for conditional enabling of debugging, etc.) Is there any simple way to do this? An environment variable, perhaps?
I know I can probably also do it the other way around - that is, have it detect if it's running on a developer machine, but that just doesn't "sound right".
-
aviraldg about 12 yearsThanks, I hadn't noticed that you could set environment variables that way yet. This seems like the right way to do it.
-
Martín Coll almost 10 yearsshortcut: on_heroku = 'DYNO' in os.environ
-
crobar over 9 yearsfor security you should probably be
DEBUG=False
by default if you're doing this surely. Something likeDEBUG=False; if not 'DYNO' in os.environ: debug=True
perhaps? -
OJFord over 8 yearsI prefer the
DYNO
solution (or set this on the web UI, not withconfig:set
) since this will beTrue
onheroku local
too, meaning we can't use it to test if running on localhost or not. -
patr1ck about 8 yearsDo NOT use on_heroku = 'DYNO' in os.environ as suggested by tinchou. That environment variable is not set during certain buildpack actions, such as when collectstatic automatically runs for a django build. This is dang near impossible to debug – you're much better off using the above solution.
-
Ciro Santilli OurBigBook.com almost 8 years@Downvoters please explain so I can learn and improve info ;-)
-
Mandar Vaze almost 8 years@OllieFord I don't get
DYNO
onheroku local
, so I had to explictly addDYNO=Dummy
in my.env
(Any value is fine since we are checking just the existence of the env. variable) -
Rebs almost 8 yearsThe environment variable
DATABASE_URL
is becoming more commonplace and not used only by Heroku. It's less and less likely to be accurate as time goes on. -
Nilpo almost 7 yearsIsn't
os.environ.get('YOUR_ENV_VAR')
the suggested way of check for the existence of an environment variable? -
ascoder over 2 yearsSimple yet effective :)
-
foobored about 2 yearsIn 2022 there is also the env var
HEROKU_APP_DIR
available - at least for PHP apps using Apache. I like it more as it clearly statesHEROKU
as its source.