Package: envbash
Python module for sourcing a bash script to augment the environment.
Supports Python 2.7 and 3.4+Rationale12-factor appsrequireconfiguration loaded
from the environment.That’seasy on a platform like
Heroku, where the
environment is preset by the user with commands likeheroku config:set. But it’s messier in development and non-Heroku
deployments, where the environment might need to be loaded from a file.This package provides a mechanism for sourcing a Bash script to update
Python’s environment (os.environ). Commonly the external file is calledenv.bash, hence the name of this project.InstallationInstall fromPyPI:pipinstallenvbashUsageCallload_envbashto source a Bash script into the current Python process.
Any variables that are set in the script, regardless of whether they are
explicitly exported, will be added to the process environment.For example, givenenv.bashwith the following content:FOO='bar baz qux'This can be loaded into Python:importosfromenvbashimportload_envbashload_envbash('env.bash')print(os.environ['FOO'])#=> bar baz quxFAQHow is this different fromdotenv?Both projects aim to solve the same problem, but differ in approach. In
particular, dotenv uses an ad hoc config syntax whereas envbash uses
Bash.dotenv’s syntax becomes a problem with multi-line strings. dotenv intends for
the.envfile to be readable by the shell, but the dotenv format for
multi-line strings isn’t compatible with the shell.If the point is to have a configuration language that’s well-suited to
environment variables, it’s hard to beat pure Bash, and it’s guaranteed
to source properly into the shell.Should I commitenv.bashto source control?No, definitely not. The purpose ofenv.bashis to store development
configuration that isn’t suitable for committing to the repository,
whether that’s secret keys or developer-specific customizations. In
fact, you should add the following line to.gitignore:/env.bashIs it necessary to explicitlyexportvariables inenv.bash?No, envbash prefixes sourcing yourenv.bashwithset-awhich
causes all newly-set variables to be exported automatically. If you
would rather explicitly export variables, you canset +aat the top
of yourenv.bash.How do I put a multi-line string intoenv.bash?You can put newlines directly into a multi-line string in Bash, so for
example this works:PRIVATE_KEY="
-----BEGIN RSA PRIVATE KEY-----
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
-----END RSA PRIVATE KEY-----"Does envbash override my environment settings?By default your local environment settings win, so you can override the
content ofenv.bashby explicitly exporting variables in your shell.You can change this behavior. This makes sense for a deployed instance
that gets full configuration fromenv.bashand needs to be protected
from the calling environment.load_envbash('env.bash',override=True)Can I remove settings from the environment?By default envbash doesn’t remove settings, but you can change this
behavior.load_envbash('env.bash',remove=True)This will cause any variables that you explicitlyunsetinenv.bashto be removed from Python’sos.environas well.How do I sourceenv.bashinto my guest shell environment?Assuming that your source directory is available on the default/vagrantmount point in the guest, you can add add this line at the
bottom of/home/vagrant/.bash_profile:set -a; source /vagrant/env.bash; set +aNote that this means that settings are loaded onvagrant sshso you
need to exit the shell and rerunvagrant sshto refresh if you
change settings.What about Ruby?Seeenvbash-rubyLegalCopyright 2017-2018Scampersand LLCReleased under theMIT license