Skip to content

Instantly share code, notes, and snippets.

@hjwp
Last active August 29, 2015 14:21
Show Gist options
  • Save hjwp/0d29ec23940438a2d5e3 to your computer and use it in GitHub Desktop.
Save hjwp/0d29ec23940438a2d5e3 to your computer and use it in GitHub Desktop.
Load environment variables from bash into current python process
import os
import subprocess
script = '/home/harry/.virtualenvs/my-project-name/bin/postactivate' # eg
env_to_json = 'import os, json; print(json.dumps(dict(os.environ)))'
environ_json = subprocess.check_output(
'. {} && python -c "{}"'.format(script, env_to_json),
shell=True, env={}
)
os.environ.update(json.loads(environ_json.decode('utf8')))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment