Skip to main content

Source env.bash script to update environment

Project description

PyPI Build Status Coverage Report Python Versions

Python module for sourcing a bash script to augment the environment. Supports Python 2.7 and 3.4+

Rationale

12-factor apps require configuration loaded from the environment.

That’s easy on a platform like Heroku, where the environment is preset by the user with commands like heroku 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 called env.bash, hence the name of this project.

Installation

Install from PyPI:

pip install envbash

Usage

Call load_envbash to 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, given env.bash with the following content:

FOO='bar baz qux'

This can be loaded into Python:

import os
from envbash import load_envbash

load_envbash('env.bash')

print(os.environ['FOO'])  #=> bar baz qux

FAQ

How is this different from dotenv?

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 .env file 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 commit env.bash to source control?

No, definitely not. The purpose of env.bash is 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.bash

Is it necessary to explicitly export variables in env.bash?

No, envbash prefixes sourcing your env.bash with set -a which causes all newly-set variables to be exported automatically. If you would rather explicitly export variables, you can set +a at the top of your env.bash.

How do I put a multi-line string into env.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 of env.bash by explicitly exporting variables in your shell.

You can change this behavior. This makes sense for a deployed instance that gets full configuration from env.bash and 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 explicitly unset in env.bash to be removed from Python’s os.environ as well.

How do I source env.bash into my guest shell environment?

Assuming that your source directory is available on the default /vagrant mount 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 +a

Note that this means that settings are loaded on vagrant ssh so you need to exit the shell and rerun vagrant ssh to refresh if you change settings.

What about Ruby?

See envbash-ruby

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

envbash-2.0.0.tar.gz (7.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

envbash-2.0.0-py3-none-any.whl (6.0 kB view details)

Uploaded Python 3

File details

Details for the file envbash-2.0.0.tar.gz.

File metadata

  • Download URL: envbash-2.0.0.tar.gz
  • Upload date:
  • Size: 7.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.3

File hashes

Hashes for envbash-2.0.0.tar.gz
Algorithm Hash digest
SHA256 5aac46a5fe82e8f8de0be6282f25cdc6f6997e8792833b1b3c956d94e8473fe8
MD5 bbe88b73ec189ebebc9719f9b83f4eea
BLAKE2b-256 2f69380824a691b51bde852b763f4f675a4fdbfd9cb66c11b10e9c6dee4d6b23

See more details on using hashes here.

File details

Details for the file envbash-2.0.0-py3-none-any.whl.

File metadata

  • Download URL: envbash-2.0.0-py3-none-any.whl
  • Upload date:
  • Size: 6.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.3

File hashes

Hashes for envbash-2.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9fffd6d305768ab116d21c8467aa54b64884c6ba10a7953fe22250e4d1342138
MD5 73b8a753a989b81dc2ea20f11ae1eb54
BLAKE2b-256 1f01b5f88b606591c545164d9643fcab9ca5bd3af57ff605ac28332e5cbd6c63

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page