Skip to content

Instantly share code, notes, and snippets.

@vlasovskikh
Created April 3, 2014 14:33
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save vlasovskikh/e8fe8e0a5c4a73048a09 to your computer and use it in GitHub Desktop.
Save vlasovskikh/e8fe8e0a5c4a73048a09 to your computer and use it in GitHub Desktop.
Source Bash file using a Python function
import os
from subprocess import Popen, PIPE
import pickle
PYTHON_DUMP_ENVIRON = """\
import sys
import os
import pickle
data = pickle.dumps(os.environ)
stdout = os.fdopen(sys.stdout.fileno(), "wb")
stdout.write(data)
"""
def source_bash_file(path):
bash_cmds = [
"source '%s'" % path,
"python -c '%s'" % PYTHON_DUMP_ENVIRON,
]
p = Popen(['bash', '-c', '&&'.join(bash_cmds)], stdout=PIPE)
stdout, _ = p.communicate()
if stdout:
environ = pickle.loads(stdout)
for k, v in environ.items():
os.environ[k] = v
@vlasovskikh
Copy link
Author

Note that path must not contain ' characters for the function to work properly.

@paulharter
Copy link

Thank you

I've been playing with how best to run my tests in PyCharm and this makes it all better:)

@DevStrikerTech
Copy link

Greetings, I'm having problem while trying to get credential from bash file, kind suggestion would be great help :

Code

import os
from subprocess import Popen, PIPE
import pickle

PYTHON_DUMP_ENVIRON = """
import sys
import os
import pickle
data = pickle.dumps(os.environ)
stdout = os.fdopen(sys.stdout.fileno(), "wb")
stdout.write(data)
"""

def source_bash_file(path):
bash_cmds = [
"source '%s'" % path,
"python -c '%s'" % PYTHON_DUMP_ENVIRON,
]
p = Popen(['bash', '-c', '&&'.join(bash_cmds)], stdout=PIPE)
stdout, _ = p.communicate()
if stdout:
environ = pickle.loads(stdout)
for k, v in environ.items():
os.environ[k] = v

source_bash_file('test.sh')
print(os.environ['PASS_USR'])

Error

Traceback (most recent call last):
File "PycharmProjects/pythonProject/envutil.py", line 29, in
source_bash_file('test.sh')
File "envutil.py", line 21, in source_bash_file
p = Popen(['bash', '-c', '&&'.join(bash_cmds)], stdout=PIPE)
File "\Python\Python38\lib\subprocess.py", line 858, in init
self._execute_child(args, executable, preexec_fn, close_fds,
File "Programs\Python\Python38\lib\subprocess.py", line 1311, in _execute_child
hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
FileNotFoundError: [WinError 2] The system cannot find the file specified

@vlasovskikh
Copy link
Author

@DevStrikerTech My script requires bash shell, as its title says: "Source Bash file using a Python function". It won't work on Windows where there is no bash by default. It might be possible to get it via cygwin, but there might be better alternatives to getting env vars on Windows.

@koffie
Copy link

koffie commented Nov 20, 2021

Thanks for this gist! I had to modify line 11 in a more recent version of python to:

data = pickle.dumps(dict(os.environ))

in order to make it work. Apparently os.environ is now wrapped in a class that cannot be pickled.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment