Search code examples
pythonfabric

Can fabric run a local script on a remote machine?


I have myfile.py on my local machine.

I want to do something like:

from fabric.api import env, run

env.host_string = 'whatever.com'

def run_script():
    run('python myfile.py')

but of course, this returns can't open file 'myfile.py': [Errno 2] No such file or directory How can I run this file remotely? Do I have to put it onto whatever.com?


Solution

  • You can first push your myfile.py to the remote machine using fabric.operations.put and then run the script like you've attempted to.

    But make sure the path to your script is either an absolute path or relative to the current directory from which the remote commands are being executed, this can be found out using cwd you can also manually cd into the remote folder using fabric.context_managers.cd