I would like to run a local python script remotely but activate the proper environment within the remote machine first. For example, if running locally I would do:

source activate myenv
python myscript.py

I thought I could do this using ssh like so:

ssh user@host "source activate myenv && python" < ./myscript.py

However, this doesn't seem to work. It produces the error message:

bash: activate: No such file or directory

What's the best way to do something like this?

  • You need to specify the full path to activate script. Alternatively, you can skip activation and just specify the full path the python binary within the venv – jordanm Aug 2 '18 at 20:24
  • whoa, pretty weird: your comment and an answer appeared at the same time.. what's the etiquette here, do i accept the answer or do you answer and then i accept? – Alex Aug 2 '18 at 20:26
  • If you feel the answer was good enough to mark as accepted, you should do so. I don't plan to create answer for this one. – jordanm Aug 2 '18 at 20:48
  • ok then i guess i will wait for someone else to answer as the current answer clearly doesn't address the question – Alex Aug 2 '18 at 21:00
  • You can also answer your own question and accept it if you have a solution. – jordanm Aug 2 '18 at 21:42

You don't need to activate the virtual env if you just want to execute a script and exit. Activation of a virtual enviroment is just a handy way to replace the python executable by adjusting the PATH1. So, the command

$ source path/to/myenv/bin/activate
$ python myscript.py
$ deactivate

can be effectively replaced with

$ path/to/myenv/bin/python myscript.py

So when running the script via ssh, just select the correct path to the myenvs python executable and you're good to go:

$ ssh user@host "path/to/myenv/bin/python" < ./myscript.py

1 Other than that, the environment variable VIRTUAL_ENV is set and the env name is prepended to PS1.

  • yeh that's what i ended up doing. thanks for the answer; sorry, can't upvote due to my rep < 15 – Alex Aug 3 '18 at 20:15
  • No worries! Glad I could help! – hoefling Aug 3 '18 at 20:19

If you can execute source activate myenv in the shell, then activate must be a file in $PATH. find that file and specify a full path. It must be present on the remote computer. Or find out what this activate script does and just send assignments to the remote computer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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