title: How to add a directory to Fish shell PATH from with a Python script?
body: <p>From within a Python-based installation script running on Fish shell and supporting Python 2.7 and 3.4+, I want to add a directory to the <code>PATH</code> environment variable if not already present. When the script exits, <code>PATH</code> should contain the new directory, both for the current Fish shell session as well as future logins.</p>
  
  <p>To achieve this, I can think of two potential approaches:</p>
  
  <ol>
  <li>Create a <code>localpath.fish</code> file in <code>~/.config/fish/conf.d/</code> containing: <code>set PATH ~/.local/bin $PATH</code></li>
  <li>Use Python’s <code>subprocess</code> to run: <code>set -U fish_user_paths ~/.local/bin $fish_user_paths</code></li>
  </ol>
  
  <p>I would prefer the latter approach, but I have tried invoking Fish via <code>subprocess</code> several different ways, all to no avail. For example:</p>
  
  <pre><code>&gt;&gt;&gt; subprocess.check_output(["fish", "-c", "echo", "Hello", "World!"], shell=True)
  </code></pre>
  
  <p>On Fish 3.0.2 and Python 3.7.4, the above yields:</p>
  
  <pre><code>&lt;W&gt; fish: Current terminal parameters have rows and/or columns set to zero.
  &lt;W&gt; fish: The stty command can be used to correct this (e.g., stty rows 80 columns 24).
  </code></pre>
  
  <p>… and the Python REPL prompt does not re-appear. Tapping CTRL-C does not exit properly, leaving the Python REPL in a mostly unusable state until quit and relaunched. (This appears to be a <a href="https://github.com/fish-shell/fish-shell/issues/5909" rel="nofollow noreferrer">known issue</a>.)</p>
  
  <p>Similarly, using <code>subprocess.run()</code> with Python 3.7’s <code>capture_output</code> parameter fails to yield the expected output:</p>
  
  <pre><code>&gt;&gt;&gt; subprocess.run(["fish", "-c", "echo", "Hello", "World!"], capture_output=True)
  CompletedProcess(args=['fish', '-c', 'echo', 'Hello', 'World!'], returncode=0, stdout=b'\n', stderr=b'')
  </code></pre>
  
  <p>My questions:</p>
  
  <ol>
  <li>Is there a better strategy than either of the two methods above?</li>
  <li>If using the first approach, how would I adjust <code>PATH</code> for the current Fish shell session from within a Python script?</li>
  <li>If using the <code>set -U fish_user_paths […]</code> approach, what would be the appropriate way to invoke that from within a Python script?</li>
  </ol>