Package: heroku.env
heroku.envCLI tool to manipulate environment variables on Heroku with local .env files, through the Heroku API.Mentioned in the popular PyCoder’s Weekly:https://pycoders.com/issues/357This library was a quick personal project because we used to use Heroku a lot at work and I find it really annoying
when you have to add env vars manually from your local environment every time.Installationpipinstall-Uheroku.envRequirementsPython 3.7+Your Heroku API key (foundhereor by runningthis), for the Heroku API to run and authenticate you.A valid Heroku app name is required to run against.The absolute/relative path to the .env file is also needed, but if not provided,.envwill be used as the default file name, which expects a file named.envto be present in the current working directory.FeaturesAuto-setting of all env vars from a .env file.Examples:$heroku.env--appswimming-briskly-123--env-filedot.envLines starting with # are considered comments in the env file and ignored.Examples:# Django settings moduleDJANGO_SETTINGS_MODULE=portfolio.settingsAllow setting of alternate values or removal/skipping of an env var by specifyingalt_valuein the comments of the env file AND with the option--set-alt.For example, if you want to load the env vars from the .env file into a Heroku app running in production,
you might want to use different values for some variables, than the ones in the .env file.Examples of allowed usages:I use a portfolio.env for my portfolio app running in Docker for local development.
If I want the Heroku app to use a different value forDJANGO_SETTINGS_MODULE, I would do something like this:# Django settings module alt_value=portfolio.prod_settingsDJANGO_SETTINGS_MODULE=portfolio.settingsOR# Django settings module# alt_value=portfolio.prod_settingsDJANGO_SETTINGS_MODULE=portfolio.settingsOR# alt_value=portfolio.prod_settings# Django settings moduleDJANGO_SETTINGS_MODULE=portfolio.settingsNOTE: Thealt_valueneeds to be on the line right before the actual env var you want to change.
Also note thatanything(except for trailing whitespace) specified afteralt_value=is used as the alternate value, so be careful.If you want to skip an environment variable, simply don’t setalt_valueto anything (usealt_value=)
and it won’t be set on the app.
Note that this will not remove if its added already, but only skips it.For example, this variable will be skipped:# Django settings module# alt_value=DJANGO_SETTINGS_MODULE=portfolio.settingsIf you want to remove an existing environment variable, setalt_valueto-(usealt_value=-)
and it will be removed from the Heroku app.In this example, this variable will be removed:# Django settings module# alt_value=-DJANGO_SETTINGS_MODULE=portfolio.settingsAny of these methods above will force the tool to replace/skip/removeDJANGO_SETTINGS_MODULEbut only if you run with the option--set-altlike this:$heroku.env--appswimming-briskly-123--env-fileportfolio.env--set-altSecure, does not store your API key anywhere.There are 3 ways of passing your API key:setting it as an environment variable (recommended: set asHEROKU_API_KEYbeforerunning the tool)entering it in a password prompt.passing with--api-keyor-kIf the API key is set as an environment variable, it will be automatically read first.All other ways of setting it will be ignored.If it is not set in any way, you will be given a password-type prompt to enter it.It is recommended for security purposes that you set it as an environment variable before running the tool, like this:$exportHEROKU_API_KEY=a1b12c24-ab1d-123f-5678-1234b12a0a1bOR you can wait for the prompt.This isnotrecommended, but is still available as an option.$heroku.env--appswimming-briskly-123--env-filedot.env--api-keya1b12c24-ab1d-123f-5678-1234b12a0a1bDumping of all env vars from upstream Heroku for a specified app into a specified .env file.Warning: Existing .env files in the same path with the same name will be replaced.
As usual, absolute/relative path to the .env file is needed, but if not provided,.envwill be used as
the default file name.Examples:$heroku.env--dump--appswimming-briskly-123--env-filedot.envSee all options with:$heroku.env--helpCreditsThis is inspired by a script bysdkcodes.martyzz1, for theheroku3API wrapper.Click, for making writing CLI tools a complete pleasure.History0.6.1 (2023-01-11)Throw error if env file is empty0.6.0 (2023-01-11)drop Python2 support, support 3.7+remove API key confirmation promptupdate dependencies0.5.5 (2019-02-13)bug fixes/improvements0.5.4 (2019-02-12)fix –dump for absent files0.5.3 (2019-02-12)allow dumping of env varsfixes for py2.x0.5.2 (2019-01-27)allow skipping of env varsallow removal of env varscheck for rate limit0.5.1 (2019-01-08)fixes0.5.0 (2019-01-06)Major revamp: Does not require Heroku CLI anymore. Uses Heroku API instead.Added –version/-v to check versionMinor improvements0.4.0 (2018-12-30)Detect Heroku installation and issues and launch appropriate URLs in browser to guide userMinor improvements0.3.0 (2018-12-29)add feature to set alternate values during runcleanup and minor improvements0.2.0 (2018-12-24)First release on PyPI.