Save data in the context variable in Python's invoke library (pyinvoke)
<p>Using the <a href="http://docs.pyinvoke.org/en/" rel="nofollow noreferrer">Invoke library for Python</a>, I want to run a setup task first which saves some values in the <a href="http://docs.pyinvoke.org/en/0.13.0/api/context.html" rel="nofollow noreferrer">context variable</a> that all of the following tasks then can read and use.</p>

<p><a href="http://docs.pyinvoke.org/en/0.13.0/concepts/context.html" rel="nofollow noreferrer">The documentation is quite short on that topic</a>, however, it states that</p>

<blockquote>
  <p>A common problem task runners face is transmission or storage of
  values which are “global” for the current session - values loaded from
  configuration files (or other configuration vectors), CLI flags,
  <strong>values set by ‘setup’ tasks</strong>, etc.</p>
  
  <p>(emphasis by me)</p>
</blockquote>

<p>, so that should actually be possible.</p>

<p>However, when I write this,</p>

<pre><code>from invoke import task

@task
def set_context(context):
    context.foo = 'bar'

@task
def read_context(context):
    print(context.foo)
</code></pre>

<p>and run the file from the command line via <code>invoke read_context set_context</code> , then I get</p>

<pre><code>Traceback (most recent call last):
  File "/usr/local/lib/python3.4/dist-packages/invoke/config.py", line 69, in __getattr__
    return self._get(key)
  File "/usr/local/lib/python3.4/dist-packages/invoke/config.py", line 113, in _get
    value = self.config[key]
  File "/usr/local/lib/python3.4/dist-packages/invoke/config.py", line 110, in __getitem__
    return self._get(key)
  File "/usr/local/lib/python3.4/dist-packages/invoke/config.py", line 113, in _get
    value = self.config[key]
KeyError: 'foo'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/bin/invoke", line 11, in &lt;module&gt;
    sys.exit(program.run())
  File "/usr/local/lib/python3.4/dist-packages/invoke/program.py", line 270, in run
    self.execute()
  File "/usr/local/lib/python3.4/dist-packages/invoke/program.py", line 381, in execute
    executor.execute(*self.tasks)
  File "/usr/local/lib/python3.4/dist-packages/invoke/executor.py", line 113, in execute
    result = call.task(*args, **call.kwargs)
  File "/usr/local/lib/python3.4/dist-packages/invoke/tasks.py", line 111, in __call__
    result = self.body(*args, **kwargs)
  File "/home/jenkins/workspace/Deploy_Birmingham_URL_Service_on_Docker_host/deployment/tasks.py", line 14, in read_context
    print(context.foo)
  File "/usr/local/lib/python3.4/dist-packages/invoke/config.py", line 81, in __getattr__
    raise AttributeError(err)
AttributeError: No attribute or config key found for 'foo'

Valid keys: ['run', 'tasks']

Valid real attributes: ['from_data', 'run']
</code></pre>

<p>What is the correct way in <code>invoke</code> to save something in the context variable and re-use it in other tasks? I am using Python 3.5.2 and invoke 0.13.0 if that's important.</p>

<hr>

<p>Note to future users: 0.15 fixed this (<a href="https://github.com/pyinvoke/invoke/issues/413" rel="nofollow noreferrer">github bug link)</a></p>