A very convenient way to execute a Python script on a remote server is to pipe it to ssh:
cat script.py | ssh user@address.com python -
where the -
seems to be optional.
How can I execute other bash commands before running the Python script in this way?
This does not work:
cat script.py | ssh user@address.com "cd ..; python -" # WRONG!
Interestingly, this sends a non-deterministically corrupted version of the Python script, which gives a syntax error in a different place every time you run it!
You can create a sub-shell:
cat script.py | ssh user@address.com "(cd ..; python -)"
Or a temporary file:
cat script.py | ssh user@address.com "tee >/tmp/tmp.py; cd ..; python /tmp/tmp.py; rm /tmp/tmp.py"