5

This is my fabric code:

from fabric import Connection, task

server = Connection(host="[email protected]:22", connect_kwargs={"password": "mypassword"})

@task
def dostuff(somethingmustbehere):
    server.run("uname -a")

This code works just fine. When I execute fab dostuff it does what I want it to do.

When I remove somethingmustbehere however I get this error message:

    raise TypeError("Tasks must have an initial Context argument!")
TypeError: Tasks must have an initial Context argument!

I never defined somethingmustbehere anywhere in my code. I just put it in and the error is gone and everything works. But why? What is this variable? Why do I need it? Why is it so important? And if it is so important why can it just be empty? I am really lost here. Yes it works, but I cannot run code that I don't understand. It drives me insane. :-)

Please be aware that I'm talking about the Python 3(!) version of Fabric! The Fabric version is 2.4.0

2
  • Which version of Fabric are you using? (The version number of Fabric itself, not the Python version)
    – mkrieger1
    Oct 15, 2018 at 20:38
  • Hey I'm using 2.4.0 I also added this to the original post so it is more clear to others as well. Oct 15, 2018 at 20:46

1 Answer 1

6

To be able to run a @task you need a context argument. Fabric uses invoke task() which expects to see a context object. Normally we name the variable c or ctx (which I always use to make it more clear). I don't prefer using c because I use it normally for connection

Check this line on github from invoke package repo, you will see that it raises an exception when the context argument is not present, but it doesn't explain why!

To know more about Context object, what it 's and why we need it, you can read the following on the site of pyinvoke:

Aside: what exactly is this ‘context’ arg anyway? A common problem task runners face is transmission of “global” data - values loaded from configuration files or other configuration vectors, given via CLI flags, generated in ‘setup’ tasks, etc.

Some libraries (such as Fabric 1.x) implement this via module-level attributes, which makes testing difficult and error prone, limits concurrency, and increases implementation complexity.

Invoke encapsulates state in explicit Context objects, handed to tasks when they execute . The context is the primary API endpoint, offering methods which honor the current state (such as Context.run) as well as access to that state itself.

Check these both links :

To be honest, I wasted a lot of time figuring out what context is and why my code wouldn't run without it. But at some point I just gave up and started using to make my code run without errors.

4
  • 2
    "To be honest, I wasted a lot of time figuring out what context is and why my code wouldn't run without it. But at some point I just gave up and started using to make my code run without errors." Haha, my experience exactly. Thanks for your answer!! Oct 17, 2018 at 23:10
  • Sorry, what do we need to do to make this thing work? The docs are not very useful.
    – Alper
    May 15, 2020 at 11:44
  • @Alper excuse me, I didn't get your question! What do you mean exactly?
    – Peshmerge
    May 16, 2020 at 8:36
  • 1
    Like, where does the context come from?
    – Alper
    May 16, 2020 at 11:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.