title: Applying source on Python script to export environment variables?
body: <p><strong>Context:</strong></p>
  <p>I'm working with a few developers, some on OSX and some on Linux. We use a collection of bash scripts to load environment variables, build docker images etc. The OSX users are using zsh instead of bash, so some tricks (specifically how we're getting the running script's directory) aren't compatible. What works on Linux (but not OSX) is:</p>
  <pre class="lang-sh prettyprint-override"><code>$ cat ./scripts/env_script.sh
  THIS_DIR=&quot;$( cd &quot;$( dirname &quot;${BASH_SOURCE[0]}&quot; )&quot; &gt;/dev/null 2&gt;&amp;1 &amp;&amp; pwd )&quot;
  # ... more variables ...
  $ source ./scripts/env_script.sh
  $ echo $THIS_DIR
  # the absolute path to the scripts directory
  </code></pre>
  <p>Some cursory testing shows this doesn't work on OSX, presumably because <code>BASH_SOURCE</code> doesn't work the same with zsh. Fine.</p>
  <p><strong>Goal:</strong></p>
  <p>We're all working with a Python, we would like to replace our bash scripts with Python.
  The same pattern is very easy in Python.</p>
  <pre class="lang-py prettyprint-override"><code>from pathlib import Path
  THIS_DIR = str(Path(__file__).parent)
  </code></pre>
  <p>Not the cleanest, but it works anywhere. Ideally we could run this and share <code>THIS_DIR</code> with the shell that called the script. Something like</p>
  <pre class="lang-sh prettyprint-override"><code>$ &lt;answer to my question&gt; ./scripts/env_script.py
  $ echo $THIS_DIR
  # absolute path to the scripts directory
  </code></pre>
  <p><strong>Question:</strong></p>
  <p>Suppose we rewrote our <code>./scripts/env_script.sh</code> into <code>./scripts/env_script.py</code> and collected a list of variables we would like to share with the calling environment.
  That is, we wrote the Python script above.</p>
  <p>Is it possible to do something akin to <code>source ./scripts/env_script.py</code> so that we can export the variables from the Python script into the calling shell?</p>
  <p><strong>What I've tried:</strong></p>
  <ul>
  <li>I found a potential duplicate <a href="https://stackoverflow.com/questions/18577555/sourcing-a-python-script">here</a> though the answer is basically 'Python runs in a sub-process, source would only work if your shell <em>is</em> Python` which makes sense but doesn't answer the question.</li>
  <li>If there isn't a nice way to exchange these variables, then another option would basically be to have the Python script spit the variables to stdout and then eval them within my shell. I.e. <code>eval $(./script/env_script.py)</code>. I guess this could work, but it feels wrong at least because I was always taught <code>eval</code> is evil.</li>
  </ul>